--- /dev/null
+image: freebsd/latest
+secrets:
+ - bb8ad521-baec-4191-80a5-d6545b8c9c35
+sources:
+ - git@codeberg.org:DNS-OARC/dnsperf.git
+environment:
+ CC: clang
+ CXX: clang++
+tasks:
+ - setup: |
+ sudo pkg install -y autoconf automake libtool pkgconf
+ - dependencies: |
+ sudo pkg install -y openssl ldns concurrencykit libnghttp2 json-c
+ - configure: |
+ cd dnsperf
+ sh autogen.sh
+ mkdir -p build
+ cd build
+ ../configure --enable-warn-all
+ - dist: |
+ cd dnsperf/build
+ make dist
+ tar zxvf dnsperf-*.tar.gz
+ cd dnsperf-*[0-9]
+ mkdir -p build
+ cd build
+ ../configure --enable-warn-all
+ - make: |
+ cd dnsperf/build/dnsperf-*[0-9]/build
+ make all test
\ No newline at end of file
--- /dev/null
+#!/bin/sh -xe
+
+mkdir -p /usr/local/src
+cd /usr/local/src
+
+pkg_add wget
+wget https://github.com/concurrencykit/ck/archive/refs/tags/0.7.2.tar.gz
+tar zxf 0.7.2.tar.gz
+cd ck-0.7.2
+./configure
+make
+make install
--- /dev/null
+image: openbsd/latest
+secrets:
+ - bb8ad521-baec-4191-80a5-d6545b8c9c35
+sources:
+ - git@codeberg.org:DNS-OARC/dnsperf.git
+environment:
+ AUTOCONF_VERSION: 2.72
+ AUTOMAKE_VERSION: 1.17
+ CC: clang
+ CXX: clang++
+tasks:
+ - setup: |
+ doas pkg_add autoconf-2.72p0 automake-1.17 libtool
+ - dependencies: |
+ doas pkg_add libldns nghttp2 json-c
+ doas dnsperf/.builds/install_ck.sh
+ - configure: |
+ cd dnsperf
+ sh autogen.sh
+ mkdir -p build
+ cd build
+ ../configure --enable-warn-all --with-extra-cflags="-I /usr/local/include" --with-extra-ldflags="-L/usr/local/lib"
+ - dist: |
+ cd dnsperf/build
+ make dist
+ tar zxvf dnsperf-*.tar.gz
+ cd dnsperf-*[0-9]
+ mkdir -p build
+ cd build
+ ../configure --enable-warn-all --with-extra-cflags="-I /usr/local/include" --with-extra-ldflags="-L/usr/local/lib"
+ - make: |
+ cd dnsperf/build/dnsperf-*[0-9]/build
+ make all test
\ No newline at end of file
--- /dev/null
+BasedOnStyle: webkit
+IndentWidth: 4
+AlignConsecutiveAssignments: true
+AlignConsecutiveDeclarations: true
+AlignOperands: true
+SortIncludes: false
--- /dev/null
+top=..
+
+all: srpm
+
+prereq: $(top)/rpmbuild
+ rpm -q git rpm-build >/dev/null || dnf -y install git rpm-build
+
+update-dist-tools: $(top)/dist-tools
+ ( cd "$(top)/dist-tools" && git pull )
+
+$(top)/dist-tools:
+ git clone https://github.com/jelu/dist-tools.git "$(top)/dist-tools"
+
+$(top)/rpmbuild:
+ mkdir -p "$(top)"/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
+
+srpm: prereq update-dist-tools
+ test -f .gitmodules && git submodule update --init || true
+ echo "$(spec)" | grep -q "develop.spec" && auto_build_number=`date --utc +%s` message="Auto build `date --utc --iso-8601=seconds`" "$(top)/dist-tools/spec-new-changelog-entry" || true
+ overwrite=yes nosign=yes "$(top)/dist-tools/create-source-packages" rpm
+ cp ../*.orig.tar.gz "$(top)/rpmbuild/SOURCES/"
+ echo "$(spec)" | grep -q "develop.spec" && rpmbuild -bs --define "%_topdir $(top)/rpmbuild" --undefine=dist rpm/*.spec || rpmbuild -bs --define "%_topdir $(top)/rpmbuild" --undefine=dist "$(spec)"
+ cp "$(top)"/rpmbuild/SRPMS/*.src.rpm "$(outdir)"
--- /dev/null
+name: test
+on: pull_request
+jobs:
+ debian:
+ runs-on: docker
+ container:
+ image: codeberg.org/dns-oarc/ci-images/debian
+ steps:
+ - uses: actions/checkout@v5
+ with:
+ ssh-key: ${{ vars.readonlykey }}
+ - name: install dependencies
+ run: |
+ apt-get install -y libssl-dev libldns-dev libck-dev libnghttp2-dev libjson-c-dev
+ - name: configure
+ run: |
+ sh autogen.sh
+ mkdir -p build
+ cd build
+ ../configure --enable-warn-all
+ - name: dist
+ run: |
+ cd build
+ make dist
+ tar zxvf dnsperf-*.tar.gz
+ cd dnsperf-*[0-9]
+ mkdir -p build
+ cd build
+ ../configure --enable-warn-all
+ - name: make
+ run: |
+ cd build/dnsperf-*[0-9]/build
+ make all test
+
+ ubuntu:
+ runs-on: docker
+ container:
+ image: codeberg.org/dns-oarc/ci-images/ubuntu
+ steps:
+ - uses: actions/checkout@v5
+ with:
+ ssh-key: ${{ vars.readonlykey }}
+ - name: install dependencies
+ run: |
+ apt-get install -y libssl-dev libldns-dev libck-dev libnghttp2-dev libjson-c-dev
+ - name: configure
+ run: |
+ sh autogen.sh
+ mkdir -p build
+ cd build
+ ../configure --enable-warn-all
+ - name: dist
+ run: |
+ cd build
+ make dist
+ tar zxvf dnsperf-*.tar.gz
+ cd dnsperf-*[0-9]
+ mkdir -p build
+ cd build
+ ../configure --enable-warn-all
+ - name: make
+ run: |
+ cd build/dnsperf-*[0-9]/build
+ make all test
+
+ centos:
+ runs-on: docker
+ container:
+ image: codeberg.org/dns-oarc/ci-images/centos
+ steps:
+ - uses: actions/checkout@v5
+ with:
+ ssh-key: ${{ vars.readonlykey }}
+ - name: install dependencies
+ run: |
+ dnf install -y openssl-devel ldns-devel ck-devel libnghttp2-devel json-c-devel
+ - name: configure
+ run: |
+ sh autogen.sh
+ mkdir -p build
+ cd build
+ ../configure --enable-warn-all
+ - name: dist
+ run: |
+ cd build
+ make dist
+ tar zxvf dnsperf-*.tar.gz
+ cd dnsperf-*[0-9]
+ mkdir -p build
+ cd build
+ ../configure --enable-warn-all
+ - name: make
+ run: |
+ cd build/dnsperf-*[0-9]/build
+ make all test
+
+ scan-build:
+ runs-on: docker
+ container:
+ image: codeberg.org/dns-oarc/ci-images/ubuntu
+ steps:
+ - uses: actions/checkout@v5
+ with:
+ ssh-key: ${{ vars.readonlykey }}
+ - name: install dependencies
+ run: |
+ apt-get install -y libssl-dev libldns-dev libck-dev libnghttp2-dev libjson-c-dev
+ - name: configure
+ run: |
+ sh autogen.sh
+ mkdir -p build
+ cd build
+ ../configure --enable-warn-all
+ - name: scan-build
+ run: |
+ cd build
+ scan-build --status-bugs make all
--- /dev/null
+# Prerequisites
+*.d
+
+# Object files
+*.o
+*.ko
+*.obj
+*.elf
+
+# Linker output
+*.ilk
+*.map
+*.exp
+
+# Precompiled Headers
+*.gch
+*.pch
+
+# Libraries
+*.lib
+*.a
+*.la
+*.lo
+
+# Shared objects (inc. Windows DLLs)
+*.dll
+*.so
+*.so.*
+*.dylib
+
+# Executables
+*.exe
+*.out
+*.app
+*.i*86
+*.x86_64
+*.hex
+
+# Debug files
+*.dSYM/
+*.su
+*.idb
+*.pdb
+
+# Kernel Module Compile Results
+*.mod*
+*.cmd
+.tmp_versions/
+modules.order
+Module.symvers
+Mkfile.old
+dkms.conf
+
+# Automake
+Makefile.in
+aclocal.m4
+ar-lib
+autom4te.cache
+compile
+config.guess
+config.sub
+configure
+configure~
+depcomp
+install-sh
+ltmain.sh
+m4/libtool.m4
+m4/ltoptions.m4
+m4/ltsugar.m4
+m4/ltversion.m4
+m4/lt~obsolete.m4
+missing
+config.h.in
+config.h.in~
+test-driver
+
+# Configure
+Makefile
+config.log
+config.status
+libtool
+.deps
+src/config.h
+src/stamp-h1
+build
+.dirstamp
+
+# Project specific files
+src/dnsperf
+src/dnsperf.1
+src/resperf
+src/resperf.1
+src/test/test-suite.log
+src/test/test*.sh.log
+src/test/test*.sh.trs
+src/test/*.dist
--- /dev/null
+2026-01-19 Jerry Lundström
+
+ Release 2.15.0
+
+ This release brings several new features and a few bug fixes.
+
+ Both `dnsperf` and `resperf` will now report statistics on receiving
+ unexpected DNS message IDs in responses.
+
+ Datafiles now support `\DDD` encoding in domain names as specified in
+ RFC1035 "5.1. Format".
+
+ Thanks to funding from Internet Systems Consortium (isc.org) statistics
+ in `dnsperf` can now be outputted as a stream of JSON objects using
+ the new option `-j`, see man-page for more details.
+
+ Bugfixes:
+ - Output correct histogram for connection latency, it was showing the histogram for response latency
+ - Fixed double new-line in some perf_log_* calls
+
+ Other changes:
+ - `dnsperf`: Fixed order of options in man-page
+ - `test`: Use valid DNS queries as largest UDP and TCP test payloads
+ - Fixed/modernize spec file
+ - Fix for gcov, was not removing old profile data from subdirectories
+ - `resperf`: Fixed scan-build inability to know fatal ends the program
+ - Fixed test 5, wait for background process instead of sleeping
+ - `test5`: Use a different port for DoH, `dumdumd` and `dumdohd` seems to interfere with each other
+ - `Dockerfile`: Track the latest LTS version of Ubuntu
+ - Migrate to Codeberg
+
+ 40adb3e Copyright
+ 9ce0a3f openSUSE/SLE packages
+ 3cef733 json-c
+ 09d9e5d quoting
+ ab4632d json statistics
+ f3e95e6 json statistics
+ 4952b6c domain name quoting
+ 4145323 Conn Latency histogram
+ b3cd9c6 Track the latest LTS version of Ubuntu in the Dockerfile
+ 0b02010 fix scan-build
+ d9f2df9 CI updates
+ bd1fdf3 CI
+ 0bf22cd bsd builds, debian build
+ 6d67d0d builds
+ 12d6789 Migrate to Codeberg
+ 0585df1 unexpected
+ 5748811 Add stats for unexpected message IDs
+ c4a3515 test 5
+ acfe3bc test5
+ fcb556e fix gcov
+ 15057bc SPEC
+ 3c131a7 Badges
+ 1ef5b41 largest blobs
+ ef41ffa Use valid DNS queries as largest UDP and TCP test payloads
+ 32d2a7e Workflow
+
+2024-01-18 Jerry Lundström
+
+ Release 2.14.0
+
+ This release rewords connection statistics, adds names to threads and
+ fixes a bug with using TSIG in more than one thread.
+
+ In "Connection Statistics", reconnections has been renamed to
+ connection attempts and now includes both the initial connection
+ attempt and following reconnections.
+
+ If supported, threads will now be named after what they do, such as
+ "perf-send-<num>" and "perf-recv-<num>".
+
+ The TSIG context was shared between all threads and would case a crash
+ if more than one sending thread was used. This has been fixed and TSIG
+ contexts are now per thread.
+
+ 81dc36b TSIG context per thread
+ 55011c6 Thread names
+ 8bdd480 thread names
+ 17c680d Set thread names for dnsperf
+ 54e641d Add library function to set thread names
+ fb19440 Dockerfile
+ a80de21 Reword connection statistics
+ 688a4fd Reword connection statistics
+
+2023-08-23 Jerry Lundström
+
+ Release 2.13.1
+
+ This release fixes a few issues with in-progress queries and the TCP
+ transport module.
+
+ When using stateful connections, such as TCP, if the full query couldn't
+ be sent in one go then the query and connection could get stuck as
+ "in-progress".
+ This could easily happen if you limited the in-flight queries to 1 and
+ sent very large DNS messages using the stream binary format.
+ Additional socket ready checks has been added to flush in-progress
+ queries as quickly as possible.
+
+ The TCP module's handling of errors when continuing in-progress queries
+ has been fixed. Previously it would interpret EAGAIN as an unrecoverable
+ error and trigger a reconnect, dropping the quer-y/ies in-progress for
+ that connection.
+
+ Other changes:
+ - Fix input data buffer to allow for maximum binary blob wire format, 2 byte size + max DNS message
+ - Mention PowerTools repository for building on CentOS etc
+
+ 56e180c In-progress, bitmaps, clang-format
+ bbc48db In-progress
+ 059619d In-progress
+ 752575a TCP EAGAIN
+ d51a453 Max input
+ 76764d7 64k TCP/DoT payload support
+ 8201f50 Doc
+
+2023-06-15 Jerry Lundström
+
+ Release 2.13.0
+
+ This release adds a new option `-O tls-sni=...` to set the Server Name
+ Indication when using TLS transport, currently for DNS-over-TLS and
+ DNS-over-HTTPS.
+
+ dbe84f6 TLS SNI
+ 5fb2282 Add TLS Server Name Indication extension support
+
+2023-05-21 Jerry Lundström
+
+ Release 2.12.0
+
+ This release fixes a segfault when doing DNS-over-HTTPS and changes the
+ way maximum queries per second are handled.
+
+ The DNS-over-HTTPS module handled reconnecting incorrectly and destroyed
+ the nghttp2 context during callbacks. Thanks to the help from
+ @kgillis2000 it was quickly found and fixed.
+
+ The way maximum QPS is handled has been changed by Petr Špaček @pspacek
+ (ISC). The new way solves an over-shoot problem that happened due to
+ max QPS being counted for the whole runtime and based on completed
+ queries, not just sent.
+
+ A new option `qps_threshold_wait` has also been added. This controls
+ the threshold for using `nanosleep()` between sending packet and the
+ default is measured on start-up. If the time between packets, based on
+ max QPS `-Q`, is smaller then no sleep will be performed. This improves
+ performance when doing high max QPS limiting.
+
+ Other changes:
+ - `dnsperf`: Statistics output
+ - Fixed missing connection statistics if only reconnections happened
+ - Flush output to allow pipe/grep processing
+ - Add percentage on reconnections based on total connections
+ - Support OpenSSL 3.0+
+
+ 9aca046 OpenSSL 3.0
+ 6d3d6b4 Stats, DoH
+ 316f901 qps_threshold_wait
+ ed52770 WIP: use busy wait only if necessary
+ 32229b6 WIP: import nanosleep benchmark
+ 1842b88 Fix dnsperf -Q to not overshot target value
+
+2023-03-16 Jerry Lundström
+
+ Release 2.11.2
+
+ Fixed long option argument handling (again), wasn't completely fixed
+ in v2.11.1.
+
+ e6dbd78 Long opt
+
+2023-03-10 Jerry Lundström
+
+ Release 2.11.1
+
+ Fixed long option argument handling that broke in v2.11.0 and enhanced
+ long option help text, now includes argument type.
+
+ 034e893 Long opt
+
+2023-02-08 Jerry Lundström
+
+ Release 2.11.0
+
+ A couple of new features this release courtesy of Petr Špaček
+ @pspacek (ISC) and a bunch of bugfixes.
+
+ First off `dnsperf` can now do verbose statistics during each interval
+ when using `-S`, see long option `verbose-interval-stats`.
+
+ Next is a new latency histogram output using data structures (hg64)
+ created by Tony Finch @fanf2 (ISC), see long option
+ `latency-histogram`. These will also be shown during `-S` when used
+ together with `verbose-interval-stats`.
+
+ Lastly a small change to boolean long options, they no longer require
+ a value (`=yes`) to be enabled.
+
+ Bugfixes:
+ - Make sure the number of outstanding queries don't go negative,
+ happens if queries are timed out before being sent
+ - Fixed #208:
+ - `recv_one()`: Fix handling errno, only store EAGAIN if no other
+ error has been received
+ - `do_recv()`: Don't break on error as it will count it as a received
+ message
+ - Treat `EBADF` as `EAGAIN` for stateful connections, receive thread
+ might read from a closed socket if send thread is reconnecting
+ - `dnsperf`: warn if -c, -T, -q, -Q values are auto-adjusted
+ - Fixed #222: don't process unexpected message if the message is
+ suppressed
+
+ 14db835 Opt arg
+ fb81481 Suppress unexpected
+ 64b8c6d stats_t initialization
+ 8290775 Fix first call to diff_stats
+ 4f8bd24 Compile support histograms
+ a9b4f04 External code, latency histograms
+ d1f4b65 add detailed latency histograms
+ 044e3a2 data structures for detailed latency histograms
+ ad3fb03 Opt, verbose statistics
+ 38bc936 Command usage warnings
+ 9f31595 Bad file descriptor
+ d3650eb Receive socket error handling
+ 84c8e72 Negative outstanding
+ e7df5e1 Clarify meaning of dnsperf -S output in the man page
+ 830eb43 verbose interval stats for dnsperf
+
+2022-11-11 Jerry Lundström
+
+ Release 2.10.0
+
+ This release adds a binary datafile (DNS wire) format to `dnsperf`
+ thanks to a contribution from Petr Špaček @pspacek (ISC).
+
+ The new binary format greatly improves performance in some cases, for
+ Petr's case it was testing dynamic updates. Switching to this
+ pre-compiled DNS wire format increased QPS from 4k to 600k, an increase
+ of 150 times throughput from `dnsperf`!
+
+ See man-page for `-B` for more information and examples how to use.
+
+ 5971cb9 Doc
+ a241068 CodeQL
+ 2a7d10a Fix CodeQL workflow
+ 0ce15f6 Fix COPR
+ 0ae4a54 CodeQL
+ f52e4b1 wire format input
+ c423aa9 wire format input
+ 2b0693d Input binary format
+
+2021-12-08 Jerry Lundström
+
+ Release 2.9.0
+
+ This release brings a new message suppression option and a way to control
+ how many queries are sent over a connection.
+
+ You can now suppress the message about receiving unexpected DNS message
+ IDs by using `-O suppress=unexpected`.
+
+ With `-O num-queries-per-conn=<num>` you can now limit the number of
+ queries sent over a connection before triggering a re-connection, see
+ the `dnsperf(3)` man-page for more information how this works.
+
+ cd9df40 Tests
+ 864a968 Queries per connection
+ 7c5d6ab Suppress unexpected
+ 8e9f4a2 Suppress
+
+2021-11-02 Jerry Lundström
+
+ Release 2.8.0
+
+ This release fixes response handling for DNS-over-HTTPS when multiple
+ responses are received within the same receive cycle, and adds a new
+ option to suppress some of the normal and verbose output.
+
+ The network model within dnsperf and resperf can not process more then
+ one response at a time from a protocol module, so a buffer has been added
+ to the DNS-over-HTTPS module to handle this situation (this already exists
+ in all other modules).
+
+ The new option `-O suppress=<message,...>` can selectively suppress parts
+ of the normal and verbose output. Some of these messages might be
+ excessively displayed due to the network model and depending on what
+ protocol you use which makes usage harder then necessary. For example
+ during TCP connection stage, if you end-point is slow to establish
+ connection you will get a lot of output saying that the socket isn't ready
+ yet.
+
+ 6a452b0 DoH response handling
+ e2828e7 Suppress
+
+2021-09-17 Jerry Lundström
+
+ Release 2.7.1
+
+ This release fixes issues with constructing wire-format DNS when the
+ domain names includes escaped characters such as `\123` or `\.`.
+
+ Other changes:
+ - Bump Debian package compat level to 10
+
+ 4873f02 DNS encoding
+ c4eccc0 debhelper
+
+2021-08-09 Jerry Lundström
+
+ Release 2.7.0
+
+ This release adds DNS-over-HTTPS support!
+
+ DNS-over-HTTPS can be used by specifying transport mode `doh` and you
+ should also look at the dnsperf(1) man-page (or `-H`) for the extended
+ options `doh-uri` and `doh-method`, which controls aspects of DoH/HTTP/2
+ that you might want to set.
+
+ Other fixes:
+ - Add check when constructing DNS packet so that total length of labels
+ does not exceed 255 bytes
+ - Fix connection/reconnection state handling for DoT transport
+ - Fix event handling by initializing them directly when opening the
+ sockets, otherwise events could have been missed which would give
+ incorrect statistics
+
+ 61b5eac Tests with dumdumd
+ d71071c Tests with dumdumd
+ b42f92e DoH sending
+ 2fa40bb Net stats
+ f7f8692 DoH fixes
+ ea62b49 DoH concurrent streams
+ 91929f1 DoH reconnect
+ 17660e6 DoH fixes
+ 5276aa6 resperf buckets
+ 585860e Packages
+ 3ffc601 Fixes
+ 1570609 Man-page
+ 6bcadc7 README
+ f81adf1 Fixes
+ 1acd71f Code structure
+ 6c47876 Fixes
+ 2d379f4 Fixes
+ 4d5384b Fixes
+ cee93b3 Initial DNS-over-HTTPS support implementation
+ 4ff3ebc Events
+ 8b24bbf DoT state
+ 6a5b5ef Fix too long name
+ 71fa09f long opts
+
+2021-05-31 Jerry Lundström
+
+ Release 2.6.0
+
+ This release adds EDNS options parameter `-E` to `resperf` and a script
+ for generating EDNS Client Subnet options (see `contrib/ecs-gen`).
+
+ d29d880 ECS opt
+ 877f31e edns option
+
+2021-03-25 Jerry Lundström
+
+ Release 2.5.2
+
+ This release tweaks the reconnect code for TCP and DoT.
+
+ For TCP, atomic operations are used to signal the need to reconnect
+ from the receiving thread to the sending, as the sending is the one in
+ charge of reconnecting.
+ This speeds up detection of connection lost which reduces the amount of
+ lost queries on a disconnect.
+
+ This change does not affect DoT as much, as the SSL context shared
+ between the threads are protected by a mutex.
+ But a bug was found in `sendto()` for DoT that could drop a query if
+ the socket was busy sending.
+
+ The connect and reconnect socket events has been split into connecting,
+ connected and reconnecting, reconnected. This is to report more correct
+ reconnect events when it comes to DoT, because the connection can be
+ lost while negotiating TLS.
+
+ Lastly, additional tests has been added for the network code.
+
+ d9e5663 net test
+ 22f49df network tests
+ 8e5b56e reconnect
+
+2021-03-22 Jerry Lundström
+
+ Release 2.5.1
+
+ This release re-adds support for TYPEnnn and ANY in the datafile, this
+ was missed during the removal of the dependency on BINDs development
+ libraries in v2.4.0.
+
+ Also note that with v2.5.0, this software now depends on Concurrency
+ Kit (ck) for atomic operations.
+
+ 924e6ea ANY
+ 0a444c0 TYPEnnn
+
+2021-03-12 Jerry Lundström
+
+ Release 2.5.0
+
+ This release adds re-connection support for TCP and DoT protocol,
+ new options to `resperf` and fixes a few bugs.
+
+ `dnsperf` and `resperf` will now try to re-connect when they lose a TCP
+ or DoT connection, and with that comes a few new statistics metrics.
+ For `dnsperf`, if a connection oriented protocol is used, it will now
+ show the total number of re-connections made and the connection latency.
+ For `resperf` it also shows the total number of re-connections made and
+ the gnuplot data now contains the total number of connections made and
+ the connection latency for each interval.
+ Beside re-connection support, improvements have been made when it comes
+ to tracking socket readiness while connections are established which
+ should generate less warnings about "socket not ready".
+
+ New `resperf` options:
+ - `-R`: Reopen the datafile if it runs out of data before the testing
+ is completed. This allows for long running tests on very small and
+ simple query datafile.
+ - `-F <fall_behind>`: Sets the maximum number of queries that can fall
+ behind being sent. `resperf` will stop when this many queries should
+ have been sent and it can be relative easy to hit if `-m <max_qps>`
+ is set too high.
+ The default is 1000 and setting it to zero (0) disables the check.
+
+ Bugfixes:
+ - Fixed port handling for host/network format when setting client side
+ port with `-x`
+ - Fix support for quoted characters, `\000` and `\.`, in domain names,
+ this was lost when removing BIND's internal development libraries
+ - Fix issue in `dnsperf`, it would loop forever if no connection could
+ be established
+ - Fix potential buffer overrun in `resperf` when using response id
+ for `queries[]`
+ - DoT: Fix bug when sending from buffer
+
+ Other changes:
+ - Always use `IPV6_V6ONLY` socket option for IPv6
+ - Add man-page on `-W` option added in v2.4.0
+ - Reformat man-pages
+ - `resperf`:
+ - Try and process more request each run to hopefully not hit max
+ outstanding so easy when high QPS
+ - Add default value to `-C` so it shows in help
+
+ 9308361 man-page format
+ 0e52fb4 man-page, opts, tuneups
+ e36211d stats
+ d9b9ba3 Response qid to index
+ 2b2c37e fixes, reconnection
+ acd31e5 dname quote
+ de8f049 net
+
+2021-02-23 Jerry Lundström
+
+ Release 2.4.2
+
+ This release fixes a few issues with reading of the datafile which
+ could lead to "ran out of data" errors.
+
+ The problem was that reading from the datafile was done before finding
+ a socket to send it on, or socket readyness, and that lead to progressing
+ the queries without really doing anything.
+ Another issues that's been fixed was that if the read lines perfectly
+ aligned with the buffer, it would be treated like EOF and caused an exit.
+
+ 9937287 resperf TLS
+ 6736956 datafile
+ 55faec6 ran out of data
+
+2021-02-09 Jerry Lundström
+
+ Release 2.4.1
+
+ This release fixes an issue with the socket readiness function that
+ could cause a buffer overflow (`-T 10 -c 2000`) due to `select()` being
+ limited to check 1023 sockets. `poll()` is now used which has no limit.
+
+ There has also been a few fixes to the contrib script `queryparse` that
+ has to do with python v2 and v3 compatibility and better exception
+ handling.
+
+ 24e5bee poll
+ 7dceca7 Handle only common exceptions
+ 5603294 Fix error on python3
+ 48fa517 TSIG
+
+2020-12-09 Jerry Lundström
+
+ Release 2.4.0
+
+ This release removes the dependency on BIND's internal development
+ libraries! This make building and packaging a lot easier and less
+ troublesome in the future.
+
+ This software now depends only on OpenSSL (for TSIG feature) with an
+ optional depend on LDNS (for dynamic updates feature).
+
+ New option:
+ - Transport mode option `-m`/`-M` now recognizes `dot` alongside `tls`
+ for encrypted DNS
+ - Added `-W` for outputting warnings and errors to stdout
+
+ Other changes / bugfixes:
+ - Fix potential memory leak of query descriptions when using verbose
+ - Only use TLS v1.2 and above for DoT/TLS
+ - Add a lot of tests
+ - Add coverage testing
+
+ d17743b datafile
+ 434bbf2 Checks, coverage, log, test IPv6
+ 9fb305f Coverage
+ 123ebf1 DOT, TLS version, Sonarcloud
+ 26df0bd BIND dependency
+ ee660e7 Sonarcloud
+ c9ea0ab base64
+ 4e9be82 TSIG
+ 4275045 EDNS, https
+ 7c3f51c BIND dependency
+ 6e1be5d ISC dependencies
+ e36f19d Buffer
+ 485cdd2 ISC mem, tests
+ 663dc24 Namespace clash
+ 2c44987 dynamic updates, edns, headers
+ 5d109b2 Disable HMAC
+ 79cae93 datafile, query desc
+ 663d814 net
+ c867de6 isc_result_t
+ 651ee5d opt
+ 7d30804 isc_result_t
+ 58ad313 ISC linked list
+ 7b4da6d Info
+ 8079ebc Tests
+ e3fb685 Tests
+ 2bb603a Tests
+ 297b23b Test
+ c4e244b Test
+ 1caac35 Makefile
+ e9f2aaa Coverage
+ 27af853 Fix typo in configure.ac
+ 521faa6 Badges
+ 8fa2ec4 LGTM
+ 75c89e5 COPR
+
+2020-05-15 Jerry Lundström
+
+ Release 2.3.4
+
+ This release adds a workaround, thanks to patch from Petr Menšík, for
+ building on systems with BIND 9.16. Also improves error handling by
+ using thread-safe `strerror_r()` instead of `strerror()`.
+
+ 88c3ef4 strerror
+ 1917f67 openSUSE Tumbleweed
+ fd39641 AS_VAR_APPEND
+ aeeef74 bind 9.16
+ 07732cd BIND, libcrypto, clang format
+ 08146e3 Add crypto library to checks
+ e4307c2 Add checks to pass with BIND 9.16
+ 3f9aed3 Prepare check for bind 9.11/9.16 return types
+
+2020-05-06 Jerry Lundström
+
+ Release 2.3.3
+
+ This release changes the behavior of `dnsperf` and `resperf` when it
+ comes to TCP and TLS connections, and updates package building using
+ COPR (thanks to patch from Petr Menšík (Red Hat)).
+
+ Connection reset or close are now treated as "try again" so that the
+ run is finished and not aborted. As SIGPIPE might be received on usage
+ of closed connections it's now blocked in `dnsperf` and handled as
+ a fatal action in `resperf`.
+
+ 62885ad SIGPIPE
+ 106c50e connection
+ 3ef0899 README
+ 61a3b1c COPR
+ 35efa27 COPR
+ 46b37a1 COPR
+ 5c126ae COPR
+ 1c51b76 Provide full URL in spec
+ 2a4dd0e Allow recreation of source archive
+ 931d6cc Do not require root for archive creation
+
+2019-08-23 Jerry Lundström
+
+ Release 2.3.2
+
+ This release fixes a buffer overflow when using TSIG and algorithms
+ with digests larger then SHA256, reported by Mukund Sivaraman. Also
+ fix build dependencies for `sqrt()`.
+
+ e54aa58 Digest
+ bca5d8d sqrt
+ d9eaa5b Package
+
+2019-07-24 Jerry Lundström
+
+ Release 2.3.1
+
+ After a report and additional confirming results the use of `poll()` in
+ the network receive code for TCP and TLS has been removed. This `poll()`
+ initially gave better results while testing in a docker container on
+ it's loopback interface but when on physical networks it reduced
+ performance to 1/12th, so it had to go.
+
+ Thanks to Brian Wellington (Akamai/Nominum) for the initial report and
+ testing, and to Jan Hák (CZ.NIC) for testing and confirming the results.
+
+ Bugfix:
+ - Fix check for having more DNS messages in the receive buffer for TCP
+ and TLS
+
+ 670db9c TCP/TLS receive
+ b8925b2 recvbuf have more
+
+2019-07-17 Jerry Lundström
+
+ Release 2.3.0
+
+ This release adds support for DNS over TCP and TLS which can be selected
+ by using the mode option for `dnsperf` and `resperf`. The default server
+ port used is now determined by the transport mode, udp/tcp port 53 and
+ tls port 853.
+
+ Note that the mode option is different between the program because it was
+ already taken for `resperf`.
+
+ `dnsperf` changes:
+ - Add `-m` for setting transport mode, `udp` (default), `tcp` or `tls`
+ - Add verbose messages about network readiness and congestion
+
+ `resperf` changes:
+ - Add `-M` for setting transport mode, `udp` (default), `tcp` or `tls`
+ - Add `-v` for verbose mode to report about network readiness and
+ congestion
+
+ ffa49cf LGTM, SonarCloud
+ 4cd5441 TLS
+ 35624d1 TCP send, socket ready loop
+ fbf76aa TCP support
+ 5988b06 Funding
+
+2019-01-28 Jerry Lundström
+
+ Release 2.2.1
+
+ The commit pulled from a fork that used `inttypes.h`, instead of ISC
+ internal types, missed to remove the old conversion specifier.
+ This was reported and fixed by Vladimír Čunát.
+
+ 9534ce1 remove visible "u" characters after numbers
+
+2019-01-25 Jerry Lundström
+
+ Release 2.2.0
+
+ First release by DNS-OARC with a rework of the code to use autotools,
+ semantic versioning 2.0 and bugfixes pulled from other's forks.
+
+ Bugfixes:
+ - Fix infinite loop in argument parsing
+ - Fix min/max latency summing for multithreaded runs
+ - Fix calculation of per_thread socket counts
+ - Fixes to queryparse
+ - Mark correctly end of file
+ - Support python3
+ - Stop looping on end of file undefinitely
+ - Fix compilation issues and work around missing `dns_fixedname_initname()`
+ - Clang `scan-build` fixes
+
+ Other changes:
+ - add "configure --with-bind" option
+ - Handle bind library changes to HMAC (see #22) and other differences
+ between versions
+ - Workaround issue on FreeBSD (see #23)
+ - Use `snprintf()` and OpenBSD's `strlcat()`
+ - Add/update build dependencies for Debia, Ubuntu, CentOS, FreeBSD
+ and OpenBSD
+
+ ae9bc91 Clang format
+ b9bb085 CI, buildbot
+ b84e41b Autotools, README, changelog
+ a2e1732 License
+ 9dcb661 Remove $Id markers, Principal Author and Reviewed tags from the
+ full source tree
+ 0677bf0 Use dns_fixedname_initname() where possible
+ d8d4696 [master] add "configure --with-bind" option to dnsperf
+ b71a280 Add deb based distros dependencies
+ 439c614 Replace custom isc_boolean_t with C standard bool type
+ 407ae7c Replace custom isc_u?intNN_t types with C99 u?intNN_t types
+ c27afd4 Replace ISC_PRINT_QUADFORMAT with inttypes.h format constants
+ 6fdb2f7 Fix queryparse
+ 4909b78 README
+ 2782d50 README.md: Rectify link to software
+ e31ddf4 fix calculation of per_thread socket counts
+ 3bd7fb4 Fix min/max latency summing for multithreaded runs
+ 2207e27 Fix infinite loop in argument parsing.
+ 3bfe97a Include the github URL; remove the bug reports section.
+ 0cee04a Add note about bug reports.
+ 62c4b32 add .gitignore
+ c45f0be Initial import.
+ 149172b Initial commit
+
+2015-12-15
+
+ Release 2.1.0.0
+
+ In addition to various bug fixes, the following new capabilities
+ were added in this release:
+ - The `-C` option was added to `resperf`. This option enables the local
+ server to act as multiple clients. By default, the local server
+ acts as a single client.
+ - the `-T` option was added to `dnsperf`. This option separates the
+ number of clients from the number of threads and allows more
+ clients to be simulated effectively. Note that using this option
+ impacts CPU and memory, so we recommend limiting the number of
+ threads.
+
+2012-03-01
+
+ Release 2.0.0.0
+
+ In the `dnsperf` command, the following changes occurred:
+ - The socket buffer size is no longer set to 32 kilobytes by default.
+ - A new `-c clients` option was added to enable the server to act as
+ multiple clients. Each client uses the same source IP address with a
+ unique source port. Use the `clients` argument to specify the number of
+ clients represented by the server. We recommend limiting the number of
+ clients represented by the server because the `dnsperf` process uses two
+ threads for each client (one thread for sent packets and one for
+ received packets), which impacts CPU and memory.
+ - Example query files are no longer included with the `dnsperf` program.
+ Nominum provides a sample query file that is available for download at:
+ ftp://ftp.nominum.com/pub/nominum/dnsperf/data/
+ - Latency reporting improved. When the `-v` (verbose mode) option is
+ configured with the `dnsperf` command, the command output now includes
+ latency measurements and the DNS RCODE of each response. This enables
+ users to create their own latency graphs.
+ - Performance was enhanced on modern operating systems so that faster
+ name servers can be tested.
+ - The `dnsperf` command output is enhanced to display more information in a
+ compact format.
+
+ The following options were removed from the `dnsperf` command:
+ - The `-A` option for displaying command line arguments passed to the
+ `dnsperf` tool in the final statistics output. Now, the `dnsperf` command
+ output always displays command line arguments.
+ - The `-T` option for printing a histogram showing response latency after
+ completing a test run. Now, the `-v` option enables users to include
+ latency measurements in the `dnsperf` command output.
+ - The `-H` option for configuring the number of buckets for which response
+ latency is displayed. Now, the `-v` option enables users to include
+ latency measurements in the `dnsperf` command output.
+ - The `-1` option for configuring the `dnsperf` tool to run through the input
+ file exactly one time. (Now, you use the `-n 1` option to configure the
+ `dnsperf` tool to run through the input file one time.)
+ - The `-c` option for including the number of responses received (for
+ each DNS RCODE) in the final statistics output. Now, DNS RCODE responses
+ are always reported.
+
+ In the `resperf` command, the following changes occurred:
+ - The socket buffer size is no longer set to 32 kilobytes by default.
+ - The `-A` option, which displayed command line arguments passed to the
+ `resperf` tool in the final statistics output, was removed. Now, the
+ `resperf` command output always displays command line arguments.
+
+2011-12-22
+
+ Release 1.0.2.0
+
+ This release adds support for RHEL6-64 and for Solaris 10 x86-64.
+
+ Some new configuration options have been added. You can now specify:
+ - the local port from which to send requests
+ - the local address from which to send requests
+ - the maximum number of runs through the input file, up
+ to the timeout limit.
+ - when using TSIG, algorithms other than hmac-md5 can be used.
+
+ One default has been changed:
+ - The maximum number of outstanding requests now defaults
+ to 100.
+
+ A new example query file for IPv6, `queryfile-example-ipv6`, is now
+ included with the distribution.
+
+2008-01-10
+
+ Release 1.0.1.0
+
+ This release makes binary builds of `dnsperf` available in addition to
+ the source code version previously released.
+
+ This release of `dnsperf` includes a sample query file containing
+ 100,000 queries to help with performance testing. This query file is
+ useful for checking latencies or a continuous `dnsperf` run. In the
+ binary distribution, this file is found at:
+
+ /usr/local/nom/examples/dnsperf/queryfile-example-100thousand
+
+ In the source distribution, it is at:
+
+ ./examples/queryfile-example-100thousand
+
+ where `.` is the directory made by extracting the source tarball.
+
+ Nominum recommends using a query file with at least 3 million queries
+ for a full `resperf` run as described in the man page; we make such a
+ file available for download at:
+
+ ftp://ftp.nominum.com/pub/nominum/dnsperf/data/queryfile-example-3million.gz
+
+ The following fix is included in the source distribution:
+ - 20996: `makefile.in` does not allow overriding `mandir`
+ The `--mandir` argument to `configure`, which allows the user to
+ specify the location man pages are installed, was incorrectly
+ ignored.
+
+ `queryparse` is a contributed program available in the source
+ distribution of `dnsperf`. It can be found at `contrib/queryparse/`.
+ The following changes were made to that program:
+ - 19717: `contrib/queryparse` includes outgoing queries
+ The `queryparse` script had no way of distinguishing between incoming
+ queries and outgoing queries when applied to a traffic trace from a
+ caching server. This was addressed by adding a new flag (`-r`) that,
+ when included in the command line, will keep queries with
+ `RD=0`. Otherwise, it will default to discarding them.
+ - The ability to parse responses instead of queries was added.
+ - A check was added to avoid short packets.
+ - Logic was added to detect link type and correctly set the initial
+ offset to handle both Ethernet and Cisco HDLC frames.
+ - `queryparse` now uses `pcapy` instead of the `btk` python `libcap` module.
+
+ Note that announcements of new releases of `dnsperf` are sent to the
+ mailing list: dnsperf-announce@nominum.com. To be added to the
+ mailing list, send a message to dnsperf-announce-request@nominum.com
+ with "subscribe" as the subject.
+
+ Known Issues:
+ - None.
+
+2006-12-21
+
+ Release 1.0.0.1
+
+ This release addresses the following issue in the `dnsperf` program:
+ - 18838/18782: `dnsperf` slow down issue
+ Because of an error in how timeout checking was being done, queries
+ were rarely timing out, so the number of valid queries in flight kept
+ dropping. This error has been corrected.
--- /dev/null
+ARG BASE_IMAGE=ubuntu:latest
+FROM $BASE_IMAGE AS runtime_base
+MAINTAINER Petr Spacek <pspacek@isc.org>
+ENV DEBIAN_FRONTEND=noninteractive
+
+RUN apt-get update
+RUN apt-get upgrade -y -qqq
+# dnsperf's runtime depedencies
+RUN apt-get install -y -qqq -o APT::Install-Suggests=0 -o APT::Install-Recommends=0 libck0 libldns3 libnghttp2-14
+# separate image for build, will not be tagged at the end
+FROM runtime_base AS build_stage
+RUN apt-get install -y -qqq -o APT::Install-Suggests=0 -o APT::Install-Recommends=0 autoconf automake build-essential git libck-dev libldns-dev libnghttp2-dev libssl-dev libtool pkg-config # copy repo as build context
+COPY . /dnsperf
+WORKDIR /dnsperf
+RUN ./autogen.sh
+RUN ./configure --prefix=/usr/local
+RUN make -j$(nproc)
+RUN make install
+RUN git log -1 > /usr/local/dnsperf.git.log
+RUN git diff > /usr/local/dnsperf.git.diff
+RUN git status > /usr/local/dnsperf.git.status
+
+# copy only installed artifacts and throw away everything else
+FROM runtime_base AS installed
+COPY --from=build_stage /usr/local /usr/local
+
+ENTRYPOINT ["/usr/local/bin/dnsperf"]
--- /dev/null
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
--- /dev/null
+# Copyright 2019-2026 OARC, Inc.
+# Copyright 2017-2018 Akamai Technologies
+# Copyright 2006-2016 Nominum, Inc.
+# All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+ACLOCAL_AMFLAGS = -I m4
+
+MAINTAINERCLEANFILES = $(srcdir)/Makefile.in \
+ $(srcdir)/src/config.h.in~ \
+ $(srcdir)/configure
+
+SUBDIRS = src
+
+dist_doc_DATA = CHANGES README.md LICENSE
+
+EXTRA_DIST = m4 contrib
+
+test: check
--- /dev/null
+# dnsperf
+
+[](https://sonarcloud.io/summary/new_code?id=dns-oarc%3Adnsperf) [](https://sonarcloud.io/summary/new_code?id=dns-oarc%3Adnsperf)
+
+`dnsperf` and `resperf` are free tools developed by Nominum/Akamai (2006-2018)
+and DNS-OARC (since 2019) that make it simple to gather accurate latency and
+throughput metrics for Domain Name Service (DNS). These tools are easy-to-use
+and simulate typical Internet, so network operators can benchmark their naming
+and addressing infrastructure and plan for upgrades. The latest version of
+the `dnsperf` and `resperf` can be used with test files that include IPv6
+queries.
+
+`dnsperf` "self-paces" the DNS query load to simulate network conditions.
+New features in `dnsperf` improve the precision of latency measurements and
+allow for per packet per-query latency reporting is possible. `dnsperf` is
+now multithreaded, multiple `dnsperf` clients can be supported in multicore
+systems (each client requires two cores). The output of `dnsperf` has also
+been improved so it is more concise and useful. Latency data can be used to
+make detailed graphs, so it is simple for network operators to take advantage
+of the data.
+
+`resperf` systematically increases the query rate and monitors the response
+rate to simulate caching DNS services.
+
+See also the `dnsperf(1)` and `resperf(1)` man pages.
+
+More information may be found here:
+- https://www.dns-oarc.net/tools/dnsperf
+
+Issues should be reported here:
+- https://codeberg.org/DNS-OARC/dnsperf/issues
+
+General support and discussion:
+- Mattermost: https://chat.dns-oarc.net/community/channels/oarc-software
+
+## Usage
+
+`dnsperf` and `resperf` read input files describing DNS queries, and send
+those queries to DNS servers to measure performance.
+
+## Dependencies
+
+`dnsperf` requires a couple of libraries beside a normal C compiling
+environment with autoconf, automake, libtool and pkgconfig.
+
+- [OpenSSL](https://www.openssl.org/) - for TSIG support
+- [Concurrency Kit](http://concurrencykit.org/) - for atomic operations
+- [LDNS](https://nlnetlabs.nl/projects/ldns/about/) - optional for dynamic update support
+- [nghttp2](https://nghttp2.org) - for DNS-over-HTTPS support using HTTP/2
+- [json-c](http://json-c.github.io/json-c/) - optional for statistics output in JSON format
+
+To install the dependencies under Debian/Ubuntu:
+```
+apt-get install -y libssl-dev libldns-dev libck-dev libnghttp2-dev libjson-c-dev
+```
+
+To install the dependencies under CentOS (with EPEL/PowerTools enabled):
+```
+yum install -y openssl-devel ldns-devel ck-devel libnghttp2-devel json-c-devel
+```
+
+To install the dependencies under FreeBSD 12+ using `pkg`:
+```
+pkg install -y openssl ldns concurrencykit libnghttp2 json-c
+```
+
+To install the dependencies under OpenBSD 6+ using `pkg_add`:
+```
+pkg_add libldns nghttp2 json-c
+```
+
+## Building from source tarball
+
+The [source tarball from DNS-OARC](https://www.dns-oarc.net/tools/dnsperf)
+comes prepared with `configure`:
+
+```
+tar zxvf dnsperf-version.tar.gz
+cd dnsperf-version
+./configure [options]
+make
+make install
+```
+
+## Building from Git repository
+
+```
+git clone https://codeberg.org/DNS-OARC/dnsperf.git
+cd dnsperf
+./autogen.sh
+./configure [options]
+make
+make install
+```
+
+## Additional Software
+
+The contrib directory contains additional software related to `dnsperf` and
+`resperf`.
+
+## License
+
+```
+Copyright 2019-2026 OARC, Inc.
+Copyright 2017-2018 Akamai Technologies
+Copyright 2006-2016 Nominum, Inc.
+All rights reserved.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+```
--- /dev/null
+#!/bin/sh -e
+# Copyright 2019-2026 OARC, Inc.
+# Copyright 2017-2018 Akamai Technologies
+# Copyright 2006-2016 Nominum, Inc.
+# All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+autoreconf --force --install --include=m4
--- /dev/null
+# Copyright 2019-2026 OARC, Inc.
+# Copyright 2017-2018 Akamai Technologies
+# Copyright 2006-2016 Nominum, Inc.
+# All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+AC_PREREQ(2.64)
+AC_INIT([dnsperf], [2.15.0], [admin@dns-oarc.net], [dnsperf], [https://codeberg.org/DNS-OARC/dnsperf/issues])
+AM_INIT_AUTOMAKE([-Wall -Werror foreign subdir-objects])
+AC_CONFIG_SRCDIR([src/dnsperf.c])
+AC_CONFIG_HEADER([src/config.h])
+AC_CONFIG_MACRO_DIR([m4])
+
+# for pthread_setname_np
+# needs to be called before any macros that run the C compiler
+AC_GNU_SOURCE
+
+# Checks for programs.
+AC_PROG_CC
+AM_PROG_CC_C_O
+AC_CANONICAL_HOST
+AC_C_INLINE
+LT_INIT([disable-static])
+
+# Check --enable-warn-all
+AC_ARG_ENABLE([warn-all], [AS_HELP_STRING([--enable-warn-all], [Enable all compiler warnings])], [AX_CFLAGS_WARN_ALL()])
+
+# Check --with-extra-cflags
+AC_ARG_WITH([extra-cflags], [AS_HELP_STRING([--with-extra-cflags=CFLAGS], [Add extra CFLAGS])], [
+ AC_MSG_NOTICE([appending extra CFLAGS... $withval])
+ AS_VAR_APPEND(CFLAGS, [" $withval"])
+])
+
+# Check --with-extra-ldflags
+AC_ARG_WITH([extra-ldflags], [AS_HELP_STRING([--with-extra-ldflags=LDFLAGS], [Add extra LDFLAGS])], [
+ AC_MSG_NOTICE([appending extra LDFLAGS... $withval])
+ AS_VAR_APPEND(LDFLAGS, [" $withval"])
+])
+
+# Check --enable-gcov
+AC_ARG_ENABLE([gcov], [AS_HELP_STRING([--enable-gcov], [Enable coverage testing])], [
+ coverage_cflags="--coverage -g -O0 -fno-inline -fno-inline-small-functions -fno-default-inline"
+ AC_MSG_NOTICE([enabling coverage testing... $coverage_cflags])
+ AS_VAR_APPEND(CFLAGS, [" $coverage_cflags"])
+])
+AM_CONDITIONAL([ENABLE_GCOV], [test "x$enable_gcov" != "xno"])
+AM_EXTRA_RECURSIVE_TARGETS([gcov])
+
+# Checks for support.
+AX_PTHREAD
+AC_CHECK_FUNCS([pthread_setname_np pthread_set_name_np])
+AC_CHECK_HEADERS([pthread_np.h], [], [], [#include <pthread.h>])
+AC_CHECK_LIB([m], [sqrt])
+AC_CHECK_HEADERS([stdatomic.h])
+AM_CONDITIONAL([HAVE_STDATOMIC], [test "x$ac_cv_header_stdatomic_h" != "xno"])
+PKG_CHECK_MODULES([json_c], [json-c >= 0.14], [AC_DEFINE([HAVE_JSON_C], [1], [Define to 1 if you have the json-c library])], [true])
+
+# Check for OpenSSL
+PKG_CHECK_MODULES([libssl], [libssl])
+PKG_CHECK_MODULES([libcrypto], [libcrypto])
+AC_CHECK_LIB([ssl], [TLS_method],
+ [AC_DEFINE([HAVE_TLS_METHOD], [1], [Define to 1 if you have the 'TLS_method' function])])
+PKG_CHECK_MODULES([ck], [ck >= 0], [
+ AS_VAR_APPEND([CFLAGS], [" $ck_CFLAGS"])
+ AS_VAR_APPEND([LIBS], [" $ck_LIBS"])
+], [
+ AC_CHECK_HEADERS([ck_ring.h ck_pr.h],, [AC_MSG_ERROR([libck headers not found])])
+ AC_CHECK_LIB([ck], [ck_array_init],, [AC_MSG_ERROR([libck not found])])
+])
+
+# Check for LDNS
+PKG_CHECK_MODULES([libldns], [libldns >= 1.7.0], [AC_DEFINE([HAVE_LDNS], [1], [Define to 1 if you have the LDNS library])], [
+ PKG_CHECK_MODULES([libldns], [ldns >= 1.7.0], [AC_DEFINE([HAVE_LDNS], [1], [Define to 1 if you have the LDNS library])], [
+ AC_MSG_NOTICE([LDNS (>= 1.7.0) not found, dynamic update support disabled])
+ ])
+])
+
+# Check for nghttp2
+PKG_CHECK_MODULES([libnghttp2], [libnghttp2 >= 0], [
+ AS_VAR_APPEND([CFLAGS], [" $nghttp2_CFLAGS"])
+ AS_VAR_APPEND([LIBS], [" $nghttp2_LIBS"])
+], [
+ AC_CHECK_HEADERS([nghttp2.h],, [AC_MSG_ERROR([nghttp2 headers not found])])
+ AC_CHECK_LIB([nghttp2], [nghttp2_session],, [AC_MSG_ERROR([nghttp2 not found])])
+])
+
+# Output Makefiles
+AC_CONFIG_FILES([
+ Makefile
+ src/Makefile
+ src/test/Makefile
+])
+AC_OUTPUT
--- /dev/null
+# dnsperf-ecs-gen
+
+Small python script to generate EDNS Client Subnet options for `dnsperf`.
+
+Requires `dnspython` to be installed.
+
+```
+$ ./dnsperf-ecs-gen 192.168.0.1/24
+-e 8:00011800c0a800
+```
--- /dev/null
+#!/usr/bin/env python3
+
+import dns.edns
+import sys
+
+if len(sys.argv) > 1:
+ opt = dns.edns.ECSOption.from_text(sys.argv[1])
+ print("-e %d:%s" % (dns.edns.ECS, opt.to_wire().hex()))
+else:
+ print("usage: dnsperf-ecs-gen.py <address/srclen[/scopelen]>")
--- /dev/null
+
+Installation
+------------
+
+Queryparse requires the dnspython and pcapy python modules. Pcapy depends
+upon the pcap library.
+
+Libpcap may be obtained from http://www.tcpdump.org/
+Dnspython may be obtained from http://www.dnspython.org/
+Pcapy may be obtained from http://oss.coresecurity.com/projects/pcapy.html
+
+Ensure queryparse is somewhere in your path.
+
+
+Usage
+-----
+queryparse -i <input file> -o <output file>
+
+ -i <input file>: the tcpdump file that will be parsed to locate DNS
+ queries.
+
+ -o <output file>: the file to which you wish to save the queries parsed
+ from <input file>. When complete, this file is suitable
+ for use as input to dnsperf.
+
+ -r Keep packets whose RD flag is not set.
+ Use this flag when parsing captures from authoritative
+ servers. When parsing captures from caching servers,
+ do not use this flag unless you also want to parse the
+ queries the server itself is sending.
+
+ -R Parse response packets (QR=1), instead of query packets
+ (QR=0).
+
+
+Queryparse takes as input a packet capture file as created by tcpdump (or any
+other program that can save data in pcap format). It parses every UDP packet,
+looking for DNS queries. When it finds a potential query, it makes every
+effort to parse it as a valid query.
+
+Once queryparse has finished, it will print a set of statistics regarding
+the capture file to STDOUT.
+
+
+NOTE: Currently, queryparse will correctly handle packets contained in either
+Ethernet frames or Cisco HDLC frames. It is not guaranteed to work with other
+framing formats.
+
--- /dev/null
+
+
+To use queryparse, you need one or more files containing pcap-formatted packet
+captures, such as those generated by tcpdump via the -w switch.
+
+Once you have such a file, call queryparse as follows:
+
+queryparse -i tcpdump.raw -o outputfile
+
+where "tcpdump.raw" is the name of the pcap-formatted packet capture file, and
+"outputfile" is the name you wish to call the saved output of queryparse.
+
+When queryparse finishes, it will print to STDOUT a count of each type of query
+encountered during its run. For example:
+
+Statistics:
+ A: 1175140
+ SOA: 23639
+ NAPTR: 113
+ NS: 1329
+ CNAME: 1667
+ NONE: 38
+ PTR: 186053
+ AAAA: 50858
+ ANY: 2117
+ SRV: 49470
+ KEY: 218
+ A6: 245
+ TXT: 24243
+ MX: 517510
+-------------------------
+ TOTAL: 2032640
+
+
+
+The resulting output is in a format suitable as input to resperf or dnsperf.
+For example:
+
+example.biz. A
+example.net. MX
+foo.example.tv. A
+example.enc. MX
+example[2].txt. MX
+foo.]. MX
+
+
+Note that there are both valid and invalid host names in the output: Neither
+queryparse nor resperf or dnsperf discriminate on the basis of a host name's
+adherence to RFCs. If the query was put on the wire and can be recognized as a
+properly-formed query, it will be saved. If this does not meet your needs, you
+may wish to parse the resulting output file to eliminate nonconforming host
+names.
--- /dev/null
+#!/usr/bin/env python
+#
+# Copyright 2019-2026 OARC, Inc.
+# Copyright 2017-2018 Akamai Technologies
+# Copyright 2006-2016 Nominum, Inc.
+# All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import dns.message
+import dns.rrset
+import dns.flags
+import dns.name
+import pcapy
+import socket
+import sys
+import struct
+from optparse import OptionParser
+
+
+__author__ = "Nominum, Inc."
+__version__ = "1.0.2.0"
+__date__ = "2007-05-14"
+
+IPHeader = '!BBHHHBBHLL'
+
+IPHDRLEN = 20
+UDPHDRLEN = 8
+LINKTYPE_C_HDLC = 104
+LINKTYPE_ETHERNET = 1
+qtypecount = {}
+
+
+def main(argv):
+ parser = OptionParser(usage="%prog [options]",
+ version = "%prog " + __version__ )
+ parser.add_option("-i", "--input", dest="fin",
+ help="name of tcpdump file to parse", metavar="FILE")
+ parser.add_option("-o", "--output", dest="fout",
+ help="file in which to save parsed DNS queries",
+ metavar="FILE")
+ parser.add_option("-r", "--recursion", dest="recurse", action="store_true",
+ default=False,
+ help="Keep queries whose RD flag is 0 (default: discard)")
+ parser.add_option("-R", "--responses", dest="responses",
+ action="store_true", default=False,
+ help="Parse query responses instead of queries")
+ (opts, args) = parser.parse_args()
+
+ if opts.fin:
+ pcap = pcapy.open_offline(opts.fin)
+ else:
+ pcap = pcapy.open_offline('-')
+ linktype = pcap.datalink()
+ if linktype == LINKTYPE_C_HDLC:
+ IPHDRSTART = 4
+ else:
+ IPHDRSTART = 14
+ if opts.fout:
+ outfile = open(opts.fout, "w")
+ else:
+ outfile = sys.stdout
+ while True:
+ try:
+ packet = pcap.next()
+ except Exception:
+ break
+
+ if packet[0] is None:
+ break
+ packet = packet[1]
+ # Toss the stuff before the IP header
+ packet = packet[IPHDRSTART:]
+
+ # Grab the rest of the packet so we can parse proto
+ iphdr = packet[0:IPHDRLEN]
+ if len(iphdr) < IPHDRLEN:
+ continue
+ (vhl, tos, tlen, ipid, fragoff, ttl, proto, cksum, srcip, dstip) = \
+ struct.unpack(IPHeader, iphdr)
+
+ # Toss the IP header, we're done with it. We need to account
+ # for any IP header options.
+ ihl = (vhl & 0xF) * 4
+ packet = packet[ihl:]
+
+ if proto == socket.IPPROTO_UDP: # UDP, 8-byte header
+ packet = packet[UDPHDRLEN:]
+ else:
+ continue
+
+ try:
+ msg = dns.message.from_wire(packet)
+ except Exception:
+ continue
+ if not opts.recurse and not dns.flags.RD:
+ continue
+ if opts.responses:
+ querytest = msg.flags & dns.flags.QR
+ else:
+ querytest = not (msg.flags & dns.flags.QR)
+ if querytest:
+ for query in msg.question: # handle multiple queries per packet
+ fqdn = query.name.to_text()
+ qtype = dns.rdatatype.to_text(query.rdtype)
+ outfile.write("%s %s\n" % (fqdn, qtype))
+ # add qtype to dict if not present, otherwise increment
+ qtypecount[query.rdtype] = qtypecount.get(query.rdtype, 0) + 1
+
+ if outfile is not sys.stdout:
+ outfile.close()
+ sum = 0
+ print("Statistics:")
+ qtypes = list(qtypecount.keys())
+ qtypes.sort()
+ for qtype in qtypes:
+ qtype_str = dns.rdatatype.to_text(qtype)
+ count = qtypecount[qtype]
+ print(" %10s:\t%d" % (qtype_str, count))
+ sum += count
+ print("-------------------------")
+ print(" TOTAL:\t%d" % sum)
+
+if __name__ == '__main__':
+ main(sys.argv[1:])
--- /dev/null
+.\" Copyright 2019-2026 OARC, Inc.
+.\" Copyright 2017-2018 Akamai Technologies
+.\" Copyright 2006-2016 Nominum, Inc.
+.\" All rights reserved.
+.\"
+.\" Licensed under the Apache License, Version 2.0 (the "License");
+.\" you may not use this file except in compliance with the License.
+.\" You may obtain a copy of the License at
+.\"
+.\" http://www.apache.org/licenses/LICENSE-2.0
+.\"
+.\" Unless required by applicable law or agreed to in writing, software
+.\" distributed under the License is distributed on an "AS IS" BASIS,
+.\" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+.\" See the License for the specific language governing permissions and
+.\" limitations under the License.
+.TH "queryparse" 1
+.SH NAME
+queryparse \- extract DNS queries from pcap capture files.
+.SH SYNOPSIS
+.B queryparse [-i
+.I input file
+.B ] [-o
+.I output file
+.B ] [-r
+.I recursion only
+.B ] [-R
+.I parse responses
+.B ]
+.SH DESCRIPTION
+.B queryparse
+is a tool designed to extract DNS queries from pcap-formatted packet
+capture files and save them in a form suitable for input to Nominum's
+dnsperf or resperf benchmarking tools.
+.B queryparse
+will only examine UDP packets, and currently supports Ethernet and Cisco HDLC frame types.
+.SH OPTIONS
+.IP "\-i filename"
+Attempt to extract DNS queries from
+.I filename,
+which should be a pcap-formatted packet capture session (e.g., a file created
+by tcpdump or ethereal).
+.IP "\-o filename"
+Write queries to
+.I filename
+in a format suitable for input to Nominum's dnsperf or resperf benchmarking tools.
+.IP "\-r"
+Keep queries that do not have the RD (recursion desired) flag set. This is useful when parsing packet captures from authoritative nameservers. When parsing captures from caching nameservers, do not use it unless you also want to parse the outgoing queries from the nameserver. Defaults to discarding queries with RD=0.
+.IP "\-R"
+Parse responses (QR=1) instead of queries (QR=0).
+.SH FILES
+None
+.SH ENVIRONMENT
+None
+.SH DIAGNOSTICS
+None
+.SH BUGS
+None
+.SH AUTHOR
+Nominum, Inc.
+.SH "SEE ALSO"
+.BR dnsperf (1),
+.BR resperf (1),
+.BR pcap (3),
+.BR tcpdump (8)
--- /dev/null
+#!/bin/sh
+
+clang-format \
+ -style=file \
+ -i \
+ src/*.c \
+ src/*.h
--- /dev/null
+# ===========================================================================
+# https://www.gnu.org/software/autoconf-archive/ax_append_flag.html
+# ===========================================================================
+#
+# SYNOPSIS
+#
+# AX_APPEND_FLAG(FLAG, [FLAGS-VARIABLE])
+#
+# DESCRIPTION
+#
+# FLAG is appended to the FLAGS-VARIABLE shell variable, with a space
+# added in between.
+#
+# If FLAGS-VARIABLE is not specified, the current language's flags (e.g.
+# CFLAGS) is used. FLAGS-VARIABLE is not changed if it already contains
+# FLAG. If FLAGS-VARIABLE is unset in the shell, it is set to exactly
+# FLAG.
+#
+# NOTE: Implementation based on AX_CFLAGS_GCC_OPTION.
+#
+# LICENSE
+#
+# Copyright (c) 2008 Guido U. Draheim <guidod@gmx.de>
+# Copyright (c) 2011 Maarten Bosmans <mkbosmans@gmail.com>
+#
+# Copying and distribution of this file, with or without modification, are
+# permitted in any medium without royalty provided the copyright notice
+# and this notice are preserved. This file is offered as-is, without any
+# warranty.
+
+#serial 8
+
+AC_DEFUN([AX_APPEND_FLAG],
+[dnl
+AC_PREREQ(2.64)dnl for _AC_LANG_PREFIX and AS_VAR_SET_IF
+AS_VAR_PUSHDEF([FLAGS], [m4_default($2,_AC_LANG_PREFIX[FLAGS])])
+AS_VAR_SET_IF(FLAGS,[
+ AS_CASE([" AS_VAR_GET(FLAGS) "],
+ [*" $1 "*], [AC_RUN_LOG([: FLAGS already contains $1])],
+ [
+ AS_VAR_APPEND(FLAGS,[" $1"])
+ AC_RUN_LOG([: FLAGS="$FLAGS"])
+ ])
+ ],
+ [
+ AS_VAR_SET(FLAGS,[$1])
+ AC_RUN_LOG([: FLAGS="$FLAGS"])
+ ])
+AS_VAR_POPDEF([FLAGS])dnl
+])dnl AX_APPEND_FLAG
--- /dev/null
+# ===========================================================================
+# https://www.gnu.org/software/autoconf-archive/ax_cflags_warn_all.html
+# ===========================================================================
+#
+# SYNOPSIS
+#
+# AX_CFLAGS_WARN_ALL [(shellvar [,default, [A/NA]])]
+# AX_CXXFLAGS_WARN_ALL [(shellvar [,default, [A/NA]])]
+# AX_FCFLAGS_WARN_ALL [(shellvar [,default, [A/NA]])]
+#
+# DESCRIPTION
+#
+# Try to find a compiler option that enables most reasonable warnings.
+#
+# For the GNU compiler it will be -Wall (and -ansi -pedantic) The result
+# is added to the shellvar being CFLAGS, CXXFLAGS, or FCFLAGS by default.
+#
+# Currently this macro knows about the GCC, Solaris, Digital Unix, AIX,
+# HP-UX, IRIX, NEC SX-5 (Super-UX 10), Cray J90 (Unicos 10.0.0.8), and
+# Intel compilers. For a given compiler, the Fortran flags are much more
+# experimental than their C equivalents.
+#
+# - $1 shell-variable-to-add-to : CFLAGS, CXXFLAGS, or FCFLAGS
+# - $2 add-value-if-not-found : nothing
+# - $3 action-if-found : add value to shellvariable
+# - $4 action-if-not-found : nothing
+#
+# NOTE: These macros depend on AX_APPEND_FLAG.
+#
+# LICENSE
+#
+# Copyright (c) 2008 Guido U. Draheim <guidod@gmx.de>
+# Copyright (c) 2010 Rhys Ulerich <rhys.ulerich@gmail.com>
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation; either version 3 of the License, or (at your
+# option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+# Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program. If not, see <https://www.gnu.org/licenses/>.
+#
+# As a special exception, the respective Autoconf Macro's copyright owner
+# gives unlimited permission to copy, distribute and modify the configure
+# scripts that are the output of Autoconf when processing the Macro. You
+# need not follow the terms of the GNU General Public License when using
+# or distributing such scripts, even though portions of the text of the
+# Macro appear in them. The GNU General Public License (GPL) does govern
+# all other use of the material that constitutes the Autoconf Macro.
+#
+# This special exception to the GPL applies to versions of the Autoconf
+# Macro released by the Autoconf Archive. When you make and distribute a
+# modified version of the Autoconf Macro, you may extend this special
+# exception to the GPL to apply to your modified version as well.
+
+#serial 16
+
+AC_DEFUN([AX_FLAGS_WARN_ALL],[dnl
+AS_VAR_PUSHDEF([FLAGS],[_AC_LANG_PREFIX[]FLAGS])dnl
+AS_VAR_PUSHDEF([VAR],[ac_cv_[]_AC_LANG_ABBREV[]flags_warn_all])dnl
+AC_CACHE_CHECK([m4_ifval($1,$1,FLAGS) for maximum warnings],
+VAR,[VAR="no, unknown"
+ac_save_[]FLAGS="$[]FLAGS"
+for ac_arg dnl
+in "-warn all % -warn all" dnl Intel
+ "-pedantic % -Wall" dnl GCC
+ "-xstrconst % -v" dnl Solaris C
+ "-std1 % -verbose -w0 -warnprotos" dnl Digital Unix
+ "-qlanglvl=ansi % -qsrcmsg -qinfo=all:noppt:noppc:noobs:nocnd" dnl AIX
+ "-ansi -ansiE % -fullwarn" dnl IRIX
+ "+ESlit % +w1" dnl HP-UX C
+ "-Xc % -pvctl[,]fullmsg" dnl NEC SX-5 (Super-UX 10)
+ "-h conform % -h msglevel 2" dnl Cray C (Unicos)
+ #
+do FLAGS="$ac_save_[]FLAGS "`echo $ac_arg | sed -e 's,%%.*,,' -e 's,%,,'`
+ AC_COMPILE_IFELSE([AC_LANG_PROGRAM],
+ [VAR=`echo $ac_arg | sed -e 's,.*% *,,'` ; break])
+done
+FLAGS="$ac_save_[]FLAGS"
+])
+AS_VAR_POPDEF([FLAGS])dnl
+AX_REQUIRE_DEFINED([AX_APPEND_FLAG])
+case ".$VAR" in
+ .ok|.ok,*) m4_ifvaln($3,$3) ;;
+ .|.no|.no,*) m4_default($4,[m4_ifval($2,[AX_APPEND_FLAG([$2], [$1])])]) ;;
+ *) m4_default($3,[AX_APPEND_FLAG([$VAR], [$1])]) ;;
+esac
+AS_VAR_POPDEF([VAR])dnl
+])dnl AX_FLAGS_WARN_ALL
+dnl implementation tactics:
+dnl the for-argument contains a list of options. The first part of
+dnl these does only exist to detect the compiler - usually it is
+dnl a global option to enable -ansi or -extrawarnings. All other
+dnl compilers will fail about it. That was needed since a lot of
+dnl compilers will give false positives for some option-syntax
+dnl like -Woption or -Xoption as they think of it is a pass-through
+dnl to later compile stages or something. The "%" is used as a
+dnl delimiter. A non-option comment can be given after "%%" marks
+dnl which will be shown but not added to the respective C/CXXFLAGS.
+
+AC_DEFUN([AX_CFLAGS_WARN_ALL],[dnl
+AC_LANG_PUSH([C])
+AX_FLAGS_WARN_ALL([$1], [$2], [$3], [$4])
+AC_LANG_POP([C])
+])
+
+AC_DEFUN([AX_CXXFLAGS_WARN_ALL],[dnl
+AC_LANG_PUSH([C++])
+AX_FLAGS_WARN_ALL([$1], [$2], [$3], [$4])
+AC_LANG_POP([C++])
+])
+
+AC_DEFUN([AX_FCFLAGS_WARN_ALL],[dnl
+AC_LANG_PUSH([Fortran])
+AX_FLAGS_WARN_ALL([$1], [$2], [$3], [$4])
+AC_LANG_POP([Fortran])
+])
--- /dev/null
+# ===========================================================================
+# https://www.gnu.org/software/autoconf-archive/ax_pthread.html
+# ===========================================================================
+#
+# SYNOPSIS
+#
+# AX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
+#
+# DESCRIPTION
+#
+# This macro figures out how to build C programs using POSIX threads. It
+# sets the PTHREAD_LIBS output variable to the threads library and linker
+# flags, and the PTHREAD_CFLAGS output variable to any special C compiler
+# flags that are needed. (The user can also force certain compiler
+# flags/libs to be tested by setting these environment variables.)
+#
+# Also sets PTHREAD_CC to any special C compiler that is needed for
+# multi-threaded programs (defaults to the value of CC otherwise). (This
+# is necessary on AIX to use the special cc_r compiler alias.)
+#
+# NOTE: You are assumed to not only compile your program with these flags,
+# but also to link with them as well. For example, you might link with
+# $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS $LIBS
+#
+# If you are only building threaded programs, you may wish to use these
+# variables in your default LIBS, CFLAGS, and CC:
+#
+# LIBS="$PTHREAD_LIBS $LIBS"
+# CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
+# CC="$PTHREAD_CC"
+#
+# In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute constant
+# has a nonstandard name, this macro defines PTHREAD_CREATE_JOINABLE to
+# that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
+#
+# Also HAVE_PTHREAD_PRIO_INHERIT is defined if pthread is found and the
+# PTHREAD_PRIO_INHERIT symbol is defined when compiling with
+# PTHREAD_CFLAGS.
+#
+# ACTION-IF-FOUND is a list of shell commands to run if a threads library
+# is found, and ACTION-IF-NOT-FOUND is a list of commands to run it if it
+# is not found. If ACTION-IF-FOUND is not specified, the default action
+# will define HAVE_PTHREAD.
+#
+# Please let the authors know if this macro fails on any platform, or if
+# you have any other suggestions or comments. This macro was based on work
+# by SGJ on autoconf scripts for FFTW (http://www.fftw.org/) (with help
+# from M. Frigo), as well as ac_pthread and hb_pthread macros posted by
+# Alejandro Forero Cuervo to the autoconf macro repository. We are also
+# grateful for the helpful feedback of numerous users.
+#
+# Updated for Autoconf 2.68 by Daniel Richard G.
+#
+# LICENSE
+#
+# Copyright (c) 2008 Steven G. Johnson <stevenj@alum.mit.edu>
+# Copyright (c) 2011 Daniel Richard G. <skunk@iSKUNK.ORG>
+#
+# This program is free software: you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation, either version 3 of the License, or (at your
+# option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+# Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program. If not, see <https://www.gnu.org/licenses/>.
+#
+# As a special exception, the respective Autoconf Macro's copyright owner
+# gives unlimited permission to copy, distribute and modify the configure
+# scripts that are the output of Autoconf when processing the Macro. You
+# need not follow the terms of the GNU General Public License when using
+# or distributing such scripts, even though portions of the text of the
+# Macro appear in them. The GNU General Public License (GPL) does govern
+# all other use of the material that constitutes the Autoconf Macro.
+#
+# This special exception to the GPL applies to versions of the Autoconf
+# Macro released by the Autoconf Archive. When you make and distribute a
+# modified version of the Autoconf Macro, you may extend this special
+# exception to the GPL to apply to your modified version as well.
+
+#serial 24
+
+AU_ALIAS([ACX_PTHREAD], [AX_PTHREAD])
+AC_DEFUN([AX_PTHREAD], [
+AC_REQUIRE([AC_CANONICAL_HOST])
+AC_REQUIRE([AC_PROG_CC])
+AC_REQUIRE([AC_PROG_SED])
+AC_LANG_PUSH([C])
+ax_pthread_ok=no
+
+# We used to check for pthread.h first, but this fails if pthread.h
+# requires special compiler flags (e.g. on Tru64 or Sequent).
+# It gets checked for in the link test anyway.
+
+# First of all, check if the user has set any of the PTHREAD_LIBS,
+# etcetera environment variables, and if threads linking works using
+# them:
+if test "x$PTHREAD_CFLAGS$PTHREAD_LIBS" != "x"; then
+ ax_pthread_save_CC="$CC"
+ ax_pthread_save_CFLAGS="$CFLAGS"
+ ax_pthread_save_LIBS="$LIBS"
+ AS_IF([test "x$PTHREAD_CC" != "x"], [CC="$PTHREAD_CC"])
+ CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
+ LIBS="$PTHREAD_LIBS $LIBS"
+ AC_MSG_CHECKING([for pthread_join using $CC $PTHREAD_CFLAGS $PTHREAD_LIBS])
+ AC_LINK_IFELSE([AC_LANG_CALL([], [pthread_join])], [ax_pthread_ok=yes])
+ AC_MSG_RESULT([$ax_pthread_ok])
+ if test "x$ax_pthread_ok" = "xno"; then
+ PTHREAD_LIBS=""
+ PTHREAD_CFLAGS=""
+ fi
+ CC="$ax_pthread_save_CC"
+ CFLAGS="$ax_pthread_save_CFLAGS"
+ LIBS="$ax_pthread_save_LIBS"
+fi
+
+# We must check for the threads library under a number of different
+# names; the ordering is very important because some systems
+# (e.g. DEC) have both -lpthread and -lpthreads, where one of the
+# libraries is broken (non-POSIX).
+
+# Create a list of thread flags to try. Items starting with a "-" are
+# C compiler flags, and other items are library names, except for "none"
+# which indicates that we try without any flags at all, and "pthread-config"
+# which is a program returning the flags for the Pth emulation library.
+
+ax_pthread_flags="pthreads none -Kthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
+
+# The ordering *is* (sometimes) important. Some notes on the
+# individual items follow:
+
+# pthreads: AIX (must check this before -lpthread)
+# none: in case threads are in libc; should be tried before -Kthread and
+# other compiler flags to prevent continual compiler warnings
+# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
+# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads), Tru64
+# (Note: HP C rejects this with "bad form for `-t' option")
+# -pthreads: Solaris/gcc (Note: HP C also rejects)
+# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
+# doesn't hurt to check since this sometimes defines pthreads and
+# -D_REENTRANT too), HP C (must be checked before -lpthread, which
+# is present but should not be used directly; and before -mthreads,
+# because the compiler interprets this as "-mt" + "-hreads")
+# -mthreads: Mingw32/gcc, Lynx/gcc
+# pthread: Linux, etcetera
+# --thread-safe: KAI C++
+# pthread-config: use pthread-config program (for GNU Pth library)
+
+case $host_os in
+
+ freebsd*)
+
+ # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
+ # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
+
+ ax_pthread_flags="-kthread lthread $ax_pthread_flags"
+ ;;
+
+ hpux*)
+
+ # From the cc(1) man page: "[-mt] Sets various -D flags to enable
+ # multi-threading and also sets -lpthread."
+
+ ax_pthread_flags="-mt -pthread pthread $ax_pthread_flags"
+ ;;
+
+ openedition*)
+
+ # IBM z/OS requires a feature-test macro to be defined in order to
+ # enable POSIX threads at all, so give the user a hint if this is
+ # not set. (We don't define these ourselves, as they can affect
+ # other portions of the system API in unpredictable ways.)
+
+ AC_EGREP_CPP([AX_PTHREAD_ZOS_MISSING],
+ [
+# if !defined(_OPEN_THREADS) && !defined(_UNIX03_THREADS)
+ AX_PTHREAD_ZOS_MISSING
+# endif
+ ],
+ [AC_MSG_WARN([IBM z/OS requires -D_OPEN_THREADS or -D_UNIX03_THREADS to enable pthreads support.])])
+ ;;
+
+ solaris*)
+
+ # On Solaris (at least, for some versions), libc contains stubbed
+ # (non-functional) versions of the pthreads routines, so link-based
+ # tests will erroneously succeed. (N.B.: The stubs are missing
+ # pthread_cleanup_push, or rather a function called by this macro,
+ # so we could check for that, but who knows whether they'll stub
+ # that too in a future libc.) So we'll check first for the
+ # standard Solaris way of linking pthreads (-mt -lpthread).
+
+ ax_pthread_flags="-mt,pthread pthread $ax_pthread_flags"
+ ;;
+esac
+
+# GCC generally uses -pthread, or -pthreads on some platforms (e.g. SPARC)
+
+AS_IF([test "x$GCC" = "xyes"],
+ [ax_pthread_flags="-pthread -pthreads $ax_pthread_flags"])
+
+# The presence of a feature test macro requesting re-entrant function
+# definitions is, on some systems, a strong hint that pthreads support is
+# correctly enabled
+
+case $host_os in
+ darwin* | hpux* | linux* | osf* | solaris*)
+ ax_pthread_check_macro="_REENTRANT"
+ ;;
+
+ aix*)
+ ax_pthread_check_macro="_THREAD_SAFE"
+ ;;
+
+ *)
+ ax_pthread_check_macro="--"
+ ;;
+esac
+AS_IF([test "x$ax_pthread_check_macro" = "x--"],
+ [ax_pthread_check_cond=0],
+ [ax_pthread_check_cond="!defined($ax_pthread_check_macro)"])
+
+# Are we compiling with Clang?
+
+AC_CACHE_CHECK([whether $CC is Clang],
+ [ax_cv_PTHREAD_CLANG],
+ [ax_cv_PTHREAD_CLANG=no
+ # Note that Autoconf sets GCC=yes for Clang as well as GCC
+ if test "x$GCC" = "xyes"; then
+ AC_EGREP_CPP([AX_PTHREAD_CC_IS_CLANG],
+ [/* Note: Clang 2.7 lacks __clang_[a-z]+__ */
+# if defined(__clang__) && defined(__llvm__)
+ AX_PTHREAD_CC_IS_CLANG
+# endif
+ ],
+ [ax_cv_PTHREAD_CLANG=yes])
+ fi
+ ])
+ax_pthread_clang="$ax_cv_PTHREAD_CLANG"
+
+ax_pthread_clang_warning=no
+
+# Clang needs special handling, because older versions handle the -pthread
+# option in a rather... idiosyncratic way
+
+if test "x$ax_pthread_clang" = "xyes"; then
+
+ # Clang takes -pthread; it has never supported any other flag
+
+ # (Note 1: This will need to be revisited if a system that Clang
+ # supports has POSIX threads in a separate library. This tends not
+ # to be the way of modern systems, but it's conceivable.)
+
+ # (Note 2: On some systems, notably Darwin, -pthread is not needed
+ # to get POSIX threads support; the API is always present and
+ # active. We could reasonably leave PTHREAD_CFLAGS empty. But
+ # -pthread does define _REENTRANT, and while the Darwin headers
+ # ignore this macro, third-party headers might not.)
+
+ PTHREAD_CFLAGS="-pthread"
+ PTHREAD_LIBS=
+
+ ax_pthread_ok=yes
+
+ # However, older versions of Clang make a point of warning the user
+ # that, in an invocation where only linking and no compilation is
+ # taking place, the -pthread option has no effect ("argument unused
+ # during compilation"). They expect -pthread to be passed in only
+ # when source code is being compiled.
+ #
+ # Problem is, this is at odds with the way Automake and most other
+ # C build frameworks function, which is that the same flags used in
+ # compilation (CFLAGS) are also used in linking. Many systems
+ # supported by AX_PTHREAD require exactly this for POSIX threads
+ # support, and in fact it is often not straightforward to specify a
+ # flag that is used only in the compilation phase and not in
+ # linking. Such a scenario is extremely rare in practice.
+ #
+ # Even though use of the -pthread flag in linking would only print
+ # a warning, this can be a nuisance for well-run software projects
+ # that build with -Werror. So if the active version of Clang has
+ # this misfeature, we search for an option to squash it.
+
+ AC_CACHE_CHECK([whether Clang needs flag to prevent "argument unused" warning when linking with -pthread],
+ [ax_cv_PTHREAD_CLANG_NO_WARN_FLAG],
+ [ax_cv_PTHREAD_CLANG_NO_WARN_FLAG=unknown
+ # Create an alternate version of $ac_link that compiles and
+ # links in two steps (.c -> .o, .o -> exe) instead of one
+ # (.c -> exe), because the warning occurs only in the second
+ # step
+ ax_pthread_save_ac_link="$ac_link"
+ ax_pthread_sed='s/conftest\.\$ac_ext/conftest.$ac_objext/g'
+ ax_pthread_link_step=`$as_echo "$ac_link" | sed "$ax_pthread_sed"`
+ ax_pthread_2step_ac_link="($ac_compile) && (echo ==== >&5) && ($ax_pthread_link_step)"
+ ax_pthread_save_CFLAGS="$CFLAGS"
+ for ax_pthread_try in '' -Qunused-arguments -Wno-unused-command-line-argument unknown; do
+ AS_IF([test "x$ax_pthread_try" = "xunknown"], [break])
+ CFLAGS="-Werror -Wunknown-warning-option $ax_pthread_try -pthread $ax_pthread_save_CFLAGS"
+ ac_link="$ax_pthread_save_ac_link"
+ AC_LINK_IFELSE([AC_LANG_SOURCE([[int main(void){return 0;}]])],
+ [ac_link="$ax_pthread_2step_ac_link"
+ AC_LINK_IFELSE([AC_LANG_SOURCE([[int main(void){return 0;}]])],
+ [break])
+ ])
+ done
+ ac_link="$ax_pthread_save_ac_link"
+ CFLAGS="$ax_pthread_save_CFLAGS"
+ AS_IF([test "x$ax_pthread_try" = "x"], [ax_pthread_try=no])
+ ax_cv_PTHREAD_CLANG_NO_WARN_FLAG="$ax_pthread_try"
+ ])
+
+ case "$ax_cv_PTHREAD_CLANG_NO_WARN_FLAG" in
+ no | unknown) ;;
+ *) PTHREAD_CFLAGS="$ax_cv_PTHREAD_CLANG_NO_WARN_FLAG $PTHREAD_CFLAGS" ;;
+ esac
+
+fi # $ax_pthread_clang = yes
+
+if test "x$ax_pthread_ok" = "xno"; then
+for ax_pthread_try_flag in $ax_pthread_flags; do
+
+ case $ax_pthread_try_flag in
+ none)
+ AC_MSG_CHECKING([whether pthreads work without any flags])
+ ;;
+
+ -mt,pthread)
+ AC_MSG_CHECKING([whether pthreads work with -mt -lpthread])
+ PTHREAD_CFLAGS="-mt"
+ PTHREAD_LIBS="-lpthread"
+ ;;
+
+ -*)
+ AC_MSG_CHECKING([whether pthreads work with $ax_pthread_try_flag])
+ PTHREAD_CFLAGS="$ax_pthread_try_flag"
+ ;;
+
+ pthread-config)
+ AC_CHECK_PROG([ax_pthread_config], [pthread-config], [yes], [no])
+ AS_IF([test "x$ax_pthread_config" = "xno"], [continue])
+ PTHREAD_CFLAGS="`pthread-config --cflags`"
+ PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
+ ;;
+
+ *)
+ AC_MSG_CHECKING([for the pthreads library -l$ax_pthread_try_flag])
+ PTHREAD_LIBS="-l$ax_pthread_try_flag"
+ ;;
+ esac
+
+ ax_pthread_save_CFLAGS="$CFLAGS"
+ ax_pthread_save_LIBS="$LIBS"
+ CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
+ LIBS="$PTHREAD_LIBS $LIBS"
+
+ # Check for various functions. We must include pthread.h,
+ # since some functions may be macros. (On the Sequent, we
+ # need a special flag -Kthread to make this header compile.)
+ # We check for pthread_join because it is in -lpthread on IRIX
+ # while pthread_create is in libc. We check for pthread_attr_init
+ # due to DEC craziness with -lpthreads. We check for
+ # pthread_cleanup_push because it is one of the few pthread
+ # functions on Solaris that doesn't have a non-functional libc stub.
+ # We try pthread_create on general principles.
+
+ AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <pthread.h>
+# if $ax_pthread_check_cond
+# error "$ax_pthread_check_macro must be defined"
+# endif
+ static void routine(void *a) { a = 0; }
+ static void *start_routine(void *a) { return a; }],
+ [pthread_t th; pthread_attr_t attr;
+ pthread_create(&th, 0, start_routine, 0);
+ pthread_join(th, 0);
+ pthread_attr_init(&attr);
+ pthread_cleanup_push(routine, 0);
+ pthread_cleanup_pop(0) /* ; */])],
+ [ax_pthread_ok=yes],
+ [])
+
+ CFLAGS="$ax_pthread_save_CFLAGS"
+ LIBS="$ax_pthread_save_LIBS"
+
+ AC_MSG_RESULT([$ax_pthread_ok])
+ AS_IF([test "x$ax_pthread_ok" = "xyes"], [break])
+
+ PTHREAD_LIBS=""
+ PTHREAD_CFLAGS=""
+done
+fi
+
+# Various other checks:
+if test "x$ax_pthread_ok" = "xyes"; then
+ ax_pthread_save_CFLAGS="$CFLAGS"
+ ax_pthread_save_LIBS="$LIBS"
+ CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
+ LIBS="$PTHREAD_LIBS $LIBS"
+
+ # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
+ AC_CACHE_CHECK([for joinable pthread attribute],
+ [ax_cv_PTHREAD_JOINABLE_ATTR],
+ [ax_cv_PTHREAD_JOINABLE_ATTR=unknown
+ for ax_pthread_attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
+ AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <pthread.h>],
+ [int attr = $ax_pthread_attr; return attr /* ; */])],
+ [ax_cv_PTHREAD_JOINABLE_ATTR=$ax_pthread_attr; break],
+ [])
+ done
+ ])
+ AS_IF([test "x$ax_cv_PTHREAD_JOINABLE_ATTR" != "xunknown" && \
+ test "x$ax_cv_PTHREAD_JOINABLE_ATTR" != "xPTHREAD_CREATE_JOINABLE" && \
+ test "x$ax_pthread_joinable_attr_defined" != "xyes"],
+ [AC_DEFINE_UNQUOTED([PTHREAD_CREATE_JOINABLE],
+ [$ax_cv_PTHREAD_JOINABLE_ATTR],
+ [Define to necessary symbol if this constant
+ uses a non-standard name on your system.])
+ ax_pthread_joinable_attr_defined=yes
+ ])
+
+ AC_CACHE_CHECK([whether more special flags are required for pthreads],
+ [ax_cv_PTHREAD_SPECIAL_FLAGS],
+ [ax_cv_PTHREAD_SPECIAL_FLAGS=no
+ case $host_os in
+ solaris*)
+ ax_cv_PTHREAD_SPECIAL_FLAGS="-D_POSIX_PTHREAD_SEMANTICS"
+ ;;
+ esac
+ ])
+ AS_IF([test "x$ax_cv_PTHREAD_SPECIAL_FLAGS" != "xno" && \
+ test "x$ax_pthread_special_flags_added" != "xyes"],
+ [PTHREAD_CFLAGS="$ax_cv_PTHREAD_SPECIAL_FLAGS $PTHREAD_CFLAGS"
+ ax_pthread_special_flags_added=yes])
+
+ AC_CACHE_CHECK([for PTHREAD_PRIO_INHERIT],
+ [ax_cv_PTHREAD_PRIO_INHERIT],
+ [AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <pthread.h>]],
+ [[int i = PTHREAD_PRIO_INHERIT;]])],
+ [ax_cv_PTHREAD_PRIO_INHERIT=yes],
+ [ax_cv_PTHREAD_PRIO_INHERIT=no])
+ ])
+ AS_IF([test "x$ax_cv_PTHREAD_PRIO_INHERIT" = "xyes" && \
+ test "x$ax_pthread_prio_inherit_defined" != "xyes"],
+ [AC_DEFINE([HAVE_PTHREAD_PRIO_INHERIT], [1], [Have PTHREAD_PRIO_INHERIT.])
+ ax_pthread_prio_inherit_defined=yes
+ ])
+
+ CFLAGS="$ax_pthread_save_CFLAGS"
+ LIBS="$ax_pthread_save_LIBS"
+
+ # More AIX lossage: compile with *_r variant
+ if test "x$GCC" != "xyes"; then
+ case $host_os in
+ aix*)
+ AS_CASE(["x/$CC"],
+ [x*/c89|x*/c89_128|x*/c99|x*/c99_128|x*/cc|x*/cc128|x*/xlc|x*/xlc_v6|x*/xlc128|x*/xlc128_v6],
+ [#handle absolute path differently from PATH based program lookup
+ AS_CASE(["x$CC"],
+ [x/*],
+ [AS_IF([AS_EXECUTABLE_P([${CC}_r])],[PTHREAD_CC="${CC}_r"])],
+ [AC_CHECK_PROGS([PTHREAD_CC],[${CC}_r],[$CC])])])
+ ;;
+ esac
+ fi
+fi
+
+test -n "$PTHREAD_CC" || PTHREAD_CC="$CC"
+
+AC_SUBST([PTHREAD_LIBS])
+AC_SUBST([PTHREAD_CFLAGS])
+AC_SUBST([PTHREAD_CC])
+
+# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
+if test "x$ax_pthread_ok" = "xyes"; then
+ ifelse([$1],,[AC_DEFINE([HAVE_PTHREAD],[1],[Define if you have POSIX threads libraries and header files.])],[$1])
+ :
+else
+ ax_pthread_ok=no
+ $2
+fi
+AC_LANG_POP
+])dnl AX_PTHREAD
--- /dev/null
+# ===========================================================================
+# https://www.gnu.org/software/autoconf-archive/ax_require_defined.html
+# ===========================================================================
+#
+# SYNOPSIS
+#
+# AX_REQUIRE_DEFINED(MACRO)
+#
+# DESCRIPTION
+#
+# AX_REQUIRE_DEFINED is a simple helper for making sure other macros have
+# been defined and thus are available for use. This avoids random issues
+# where a macro isn't expanded. Instead the configure script emits a
+# non-fatal:
+#
+# ./configure: line 1673: AX_CFLAGS_WARN_ALL: command not found
+#
+# It's like AC_REQUIRE except it doesn't expand the required macro.
+#
+# Here's an example:
+#
+# AX_REQUIRE_DEFINED([AX_CHECK_LINK_FLAG])
+#
+# LICENSE
+#
+# Copyright (c) 2014 Mike Frysinger <vapier@gentoo.org>
+#
+# Copying and distribution of this file, with or without modification, are
+# permitted in any medium without royalty provided the copyright notice
+# and this notice are preserved. This file is offered as-is, without any
+# warranty.
+
+#serial 2
+
+AC_DEFUN([AX_REQUIRE_DEFINED], [dnl
+ m4_ifndef([$1], [m4_fatal([macro ]$1[ is not defined; is a m4 file missing?])])
+])dnl AX_REQUIRE_DEFINED
--- /dev/null
+#!/bin/sh -e
+
+m4_files="ax_pthread.m4 ax_append_flag.m4 ax_cflags_warn_all.m4 ax_require_defined.m4"
+
+for ax in $m4_files; do
+ rm -f "$ax"
+ wget -O "$ax" "http://git.savannah.gnu.org/gitweb/?p=autoconf-archive.git;a=blob_plain;f=m4/$ax"
+done
--- /dev/null
+# Copyright 2019-2026 OARC, Inc.
+# Copyright 2017-2018 Akamai Technologies
+# Copyright 2006-2016 Nominum, Inc.
+# All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+AC_DEFUN([AX_TYPE_SOCKLEN_T],
+[AC_CACHE_CHECK([for socklen_t], ac_cv_type_socklen_t,
+[
+ AC_TRY_COMPILE(
+ [#include <sys/types.h>
+ #include <sys/socket.h>],
+ [socklen_t len = 42; return len;],
+ ac_cv_type_socklen_t=yes,
+ ac_cv_type_socklen_t=no)
+])
+ if test $ac_cv_type_socklen_t != yes; then
+ AC_DEFINE([socklen_t], [int], [The type of socklen_t.])
+ fi
+])
+
+AC_DEFUN([AX_SA_LEN],
+[AC_CACHE_CHECK([for sa_len], ac_cv_sa_len,
+[
+ AC_TRY_COMPILE(
+ [#include <sys/types.h>
+ #include <sys/socket.h>],
+ [struct sockaddr sa; sa.sa_len = 0;],
+ ac_cv_sa_len=yes,
+ ac_cv_sa_len=no)
+])
+ if test $ac_cv_sa_len = yes; then
+ AC_DEFINE([HAVE_SA_LEN], [1], [Set if struct sockaddr sa_len exists.])
+ fi
+])
--- /dev/null
+Name: dnsperf
+Version: 2.15.0
+Release: 1%{?dist}
+Summary: DNS Performance Testing Tool
+Group: Productivity/Networking/DNS/Utilities
+
+License: Apache-2.0
+URL: https://www.dns-oarc.net/tools/dnsperf
+# Source needs to be generated by dist-tools/create-source-packages, see
+# https://github.com/jelu/dist-tools
+Source0: %{name}_%{version}.orig.tar.gz
+
+BuildRequires: autoconf
+BuildRequires: automake
+BuildRequires: libtool
+BuildRequires: openssl-devel
+BuildRequires: pkgconfig
+BuildRequires: ldns-devel
+BuildRequires: ck-devel
+BuildRequires: libnghttp2-devel
+%if 0%{?suse_version} || 0%{?sle_version}
+BuildRequires: libjson-c-devel
+%else
+BuildRequires: json-c-devel
+%endif
+
+%description
+dnsperf and resperf are free tools developed by Nominum/Akamai (2006-2018)
+and DNS-OARC (since 2019) that make it simple to gather accurate latency and
+throughput metrics for Domain Name Service (DNS). These tools are easy-to-use
+and simulate typical Internet, so network operators can benchmark their naming
+and addressing infrastructure and plan for upgrades. The latest version of
+the dnsperf and resperf can be used with test files that include IPv6
+queries.
+
+dnsperf "self-paces" the DNS query load to simulate network conditions.
+
+New features in dnsperf improve the precision of latency measurements and
+allow for per packet per-query latency reporting is possible. dnsperf is
+now multithreaded, multiple dnsperf clients can be supported in multicore
+systems (each client requires two cores). The output of dnsperf has also
+been improved so it is more concise and useful. Latency data can be used to
+make detailed graphs, so it is simple for network operators to take advantage
+of the data.
+
+
+%package -n resperf
+Summary: DNS Resolution Performance Testing Tool
+Group: Productivity/Networking/DNS/Utilities
+
+
+%description -n resperf
+dnsperf and resperf are free tools developed by Nominum/Akamai (2006-2018)
+and DNS-OARC (since 2019) that make it simple to gather accurate latency and
+throughput metrics for Domain Name Service (DNS). These tools are easy-to-use
+and simulate typical Internet, so network operators can benchmark their naming
+and addressing infrastructure and plan for upgrades. The latest version of
+the dnsperf and resperf can be used with test files that include IPv6
+queries.
+
+resperf systematically increases the query rate and monitors the response
+rate to simulate caching DNS services.
+
+
+%prep
+%setup -q -n %{name}_%{version}
+
+
+%build
+sh autogen.sh
+%configure "--with-extra-cflags=-I /usr/include/bind"
+%make_build
+
+
+%install
+%make_install
+
+
+%check
+%make_build test
+
+
+%files
+%{_bindir}/dnsperf
+%{_datadir}/doc/*
+%{_mandir}/man1/dnsperf.*
+
+
+%files -n resperf
+%{_bindir}/resperf
+%{_bindir}/resperf-report
+%{_mandir}/man1/resperf.*
+
+
+%changelog
+* Mon Jan 19 2026 Jerry Lundström <lundstrom.jerry@gmail.com> 2.15.0-1
+- Release 2.15.0
+ * This release brings several new features and a few bug fixes.
+ * Both `dnsperf` and `resperf` will now report statistics on receiving
+ unexpected DNS message IDs in responses.
+ * Datafiles now support `\DDD` encoding in domain names as specified in
+ RFC1035 "5.1. Format".
+ * Thanks to funding from Internet Systems Consortium (isc.org) statistics
+ in `dnsperf` can now be outputted as a stream of JSON objects using
+ the new option `-j`, see man-page for more details.
+ * Bugfixes:
+ - Output correct histogram for connection latency, it was showing the histogram for response latency
+ - Fixed double new-line in some perf_log_* calls
+ * Other changes:
+ - `dnsperf`: Fixed order of options in man-page
+ - `test`: Use valid DNS queries as largest UDP and TCP test payloads
+ - Fixed/modernize spec file
+ - Fix for gcov, was not removing old profile data from subdirectories
+ - `resperf`: Fixed scan-build inability to know fatal ends the program
+ - Fixed test 5, wait for background process instead of sleeping
+ - `test5`: Use a different port for DoH, `dumdumd` and `dumdohd` seems to interfere with each other
+ - `Dockerfile`: Track the latest LTS version of Ubuntu
+ - Migrate to Codeberg
+ * Commits:
+ 40adb3e Copyright
+ 9ce0a3f openSUSE/SLE packages
+ 3cef733 json-c
+ 09d9e5d quoting
+ ab4632d json statistics
+ f3e95e6 json statistics
+ 4952b6c domain name quoting
+ 4145323 Conn Latency histogram
+ b3cd9c6 Track the latest LTS version of Ubuntu in the Dockerfile
+ 0b02010 fix scan-build
+ d9f2df9 CI updates
+ bd1fdf3 CI
+ 0bf22cd bsd builds, debian build
+ 6d67d0d builds
+ 12d6789 Migrate to Codeberg
+ 0585df1 unexpected
+ 5748811 Add stats for unexpected message IDs
+ c4a3515 test 5
+ acfe3bc test5
+ fcb556e fix gcov
+ 15057bc SPEC
+ 3c131a7 Badges
+ 1ef5b41 largest blobs
+ ef41ffa Use valid DNS queries as largest UDP and TCP test payloads
+ 32d2a7e Workflow
+* Thu Jan 18 2024 Jerry Lundström <lundstrom.jerry@gmail.com> 2.14.0-1
+- Release 2.14.0
+ * This release rewords connection statistics, adds names to threads and
+ fixes a bug with using TSIG in more than one thread.
+ * In "Connection Statistics", reconnections has been renamed to
+ connection attempts and now includes both the initial connection
+ attempt and following reconnections.
+ * If supported, threads will now be named after what they do, such as
+ "perf-send-<num>" and "perf-recv-<num>".
+ * The TSIG context was shared between all threads and would case a crash
+ if more than one sending thread was used. This has been fixed and TSIG
+ contexts are now per thread.
+ * Commits:
+ 81dc36b TSIG context per thread
+ 55011c6 Thread names
+ 8bdd480 thread names
+ 17c680d Set thread names for dnsperf
+ 54e641d Add library function to set thread names
+ fb19440 Dockerfile
+ a80de21 Reword connection statistics
+ 688a4fd Reword connection statistics
+* Wed Aug 23 2023 Jerry Lundström <lundstrom.jerry@gmail.com> 2.13.1-1
+- Release 2.13.1
+ * This release fixes a few issues with in-progress queries and the TCP
+ transport module.
+ * When using stateful connections, such as TCP, if the full query couldn't
+ be sent in one go then the query and connection could get stuck as
+ "in-progress".
+ This could easily happen if you limited the in-flight queries to 1 and
+ sent very large DNS messages using the stream binary format.
+ Additional socket ready checks has been added to flush in-progress
+ queries as quickly as possible.
+ * The TCP module's handling of errors when continuing in-progress queries
+ has been fixed. Previously it would interpret EAGAIN as an unrecoverable
+ error and trigger a reconnect, dropping the quer-y/ies in-progress for
+ that connection.
+ * Other changes:
+ - Fix input data buffer to allow for maximum binary blob wire format, 2 byte size + max DNS message
+ - Mention PowerTools repository for building on CentOS etc
+ * Commits:
+ 56e180c In-progress, bitmaps, clang-format
+ bbc48db In-progress
+ 059619d In-progress
+ 752575a TCP EAGAIN
+ d51a453 Max input
+ 76764d7 64k TCP/DoT payload support
+ 8201f50 Doc
+* Thu Jun 15 2023 Jerry Lundström <lundstrom.jerry@gmail.com> 2.13.0-1
+- Release 2.13.0
+ * This release adds a new option `-O tls-sni=...` to set the Server Name
+ Indication when using TLS transport, currently for DNS-over-TLS and
+ DNS-over-HTTPS.
+ * Commits:
+ dbe84f6 TLS SNI
+ 5fb2282 Add TLS Server Name Indication extension support
+* Sun May 21 2023 Jerry Lundström <lundstrom.jerry@gmail.com> 2.12.0-1
+- Release 2.12.0
+ * This release fixes a segfault when doing DNS-over-HTTPS and changes the
+ way maximum queries per second are handled.
+ * The DNS-over-HTTPS module handled reconnecting incorrectly and destroyed
+ the nghttp2 context during callbacks. Thanks to the help from
+ @kgillis2000 it was quickly found and fixed.
+ * The way maximum QPS is handled has been changed by Petr Špaček @pspacek
+ (ISC). The new way solves an over-shoot problem that happened due to
+ max QPS being counted for the whole runtime and based on completed
+ queries, not just sent.
+ * A new option `qps_threshold_wait` has also been added. This controls
+ the threshold for using `nanosleep()` between sending packet and the
+ default is measured on start-up. If the time between packets, based on
+ max QPS `-Q`, is smaller then no sleep will be performed. This improves
+ performance when doing high max QPS limiting.
+ * Other changes:
+ - `dnsperf`: Statistics output
+ - Fixed missing connection statistics if only reconnections happened
+ - Flush output to allow pipe/grep processing
+ - Add percentage on reconnections based on total connections
+ - Support OpenSSL 3.0+
+ * Commits:
+ 9aca046 OpenSSL 3.0
+ 6d3d6b4 Stats, DoH
+ 316f901 qps_threshold_wait
+ ed52770 WIP: use busy wait only if necessary
+ 32229b6 WIP: import nanosleep benchmark
+ 1842b88 Fix dnsperf -Q to not overshot target value
+* Thu Mar 16 2023 Jerry Lundström <lundstrom.jerry@gmail.com> 2.11.2-1
+- Release 2.11.2
+ * Fixed long option argument handling (again), wasn't completely fixed
+ in v2.11.1.
+ * Commits:
+ e6dbd78 Long opt
+* Fri Mar 10 2023 Jerry Lundström <lundstrom.jerry@gmail.com> 2.11.1-1
+- Release 2.11.1
+ * Fixed long option argument handling that broke in v2.11.0 and enhanced
+ long option help text, now includes argument type.
+ * Commits:
+ 034e893 Long opt
+* Wed Feb 08 2023 Jerry Lundström <lundstrom.jerry@gmail.com> 2.11.0-1
+- Release 2.11.0
+ * A couple of new features this release courtesy of Petr Špaček
+ @pspacek (ISC) and a bunch of bugfixes.
+ * First off `dnsperf` can now do verbose statistics during each interval
+ when using `-S`, see long option `verbose-interval-stats`.
+ * Next is a new latency histogram output using data structures (hg64)
+ created by Tony Finch @fanf2 (ISC), see long option
+ `latency-histogram`. These will also be shown during `-S` when used
+ together with `verbose-interval-stats`.
+ * Lastly a small change to boolean long options, they no longer require
+ a value (`=yes`) to be enabled.
+ * Bugfixes:
+ - Make sure the number of outstanding queries don't go negative,
+ happens if queries are timed out before being sent
+ - Fixed #208:
+ - `recv_one()`: Fix handling errno, only store EAGAIN if no other
+ error has been received
+ - `do_recv()`: Don't break on error as it will count it as a received
+ message
+ - Treat `EBADF` as `EAGAIN` for stateful connections, receive thread
+ might read from a closed socket if send thread is reconnecting
+ - `dnsperf`: warn if -c, -T, -q, -Q values are auto-adjusted
+ - Fixed #222: don't process unexpected message if the message is
+ suppressed
+ * Commits:
+ 14db835 Opt arg
+ fb81481 Suppress unexpected
+ 64b8c6d stats_t initialization
+ 8290775 Fix first call to diff_stats
+ 4f8bd24 Compile support histograms
+ a9b4f04 External code, latency histograms
+ d1f4b65 add detailed latency histograms
+ 044e3a2 data structures for detailed latency histograms
+ ad3fb03 Opt, verbose statistics
+ 38bc936 Command usage warnings
+ 9f31595 Bad file descriptor
+ d3650eb Receive socket error handling
+ 84c8e72 Negative outstanding
+ e7df5e1 Clarify meaning of dnsperf -S output in the man page
+ 830eb43 verbose interval stats for dnsperf
+* Fri Nov 11 2022 Jerry Lundström <lundstrom.jerry@gmail.com> 2.10.0-1
+- Release 2.10.0
+ * This release adds a binary datafile (DNS wire) format to `dnsperf`
+ thanks to a contribution from Petr Špaček @pspacek (ISC).
+ * The new binary format greatly improves performance in some cases, for
+ Petr's case it was testing dynamic updates. Switching to this
+ pre-compiled DNS wire format increased QPS from 4k to 600k, an increase
+ of 150 times throughput from `dnsperf`!
+ * See man-page for `-B` for more information and examples how to use.
+ * Commits:
+ 5971cb9 Doc
+ a241068 CodeQL
+ 2a7d10a Fix CodeQL workflow
+ 0ce15f6 Fix COPR
+ 0ae4a54 CodeQL
+ f52e4b1 wire format input
+ c423aa9 wire format input
+ 2b0693d Input binary format
+* Wed Dec 08 2021 Jerry Lundström <lundstrom.jerry@gmail.com> 2.9.0-1
+- Release 2.9.0
+ * This release brings a new message suppression option and a way to control
+ how many queries are sent over a connection.
+ * You can now suppress the message about receiving unexpected DNS message
+ IDs by using `-O suppress=unexpected`.
+ * With `-O num-queries-per-conn=<num>` you can now limit the number of
+ queries sent over a connection before triggering a re-connection, see
+ the `dnsperf(3)` man-page for more information how this works.
+ * Commits:
+ cd9df40 Tests
+ 864a968 Queries per connection
+ 7c5d6ab Suppress unexpected
+ 8e9f4a2 Suppress
+* Tue Nov 02 2021 Jerry Lundström <lundstrom.jerry@gmail.com> 2.8.0-1
+- Release 2.8.0
+ * This release fixes response handling for DNS-over-HTTPS when multiple
+ responses are received within the same receive cycle, and adds a new
+ option to suppress some of the normal and verbose output.
+ * The network model within dnsperf and resperf can not process more then
+ one response at a time from a protocol module, so a buffer has been added
+ to the DNS-over-HTTPS module to handle this situation (this already exists
+ in all other modules).
+ * The new option `-O suppress=<message,...>` can selectively suppress parts
+ of the normal and verbose output. Some of these messages might be
+ excessively displayed due to the network model and depending on what
+ protocol you use which makes usage harder then necessary. For example
+ during TCP connection stage, if you end-point is slow to establish
+ connection you will get a lot of output saying that the socket isn't ready
+ yet.
+ * Commits:
+ 6a452b0 DoH response handling
+ e2828e7 Suppress
+* Fri Sep 17 2021 Jerry Lundström <lundstrom.jerry@gmail.com> 2.7.1-1
+- Release 2.7.1
+ * This release fixes issues with constructing wire-format DNS when the
+ domain names includes escaped characters such as `\123` or `\.`.
+ * Other changes:
+ - Bump Debian package compat level to 10
+ * Commits:
+ 4873f02 DNS encoding
+ c4eccc0 debhelper
+* Mon Aug 09 2021 Jerry Lundström <lundstrom.jerry@gmail.com> 2.7.0-1
+- Release 2.7.0
+ * This release adds DNS-over-HTTPS support!
+ * DNS-over-HTTPS can be used by specifying transport mode `doh` and you
+ should also look at the dnsperf(1) man-page (or `-H`) for the extended
+ options `doh-uri` and `doh-method`, which controls aspects of DoH/HTTP/2
+ that you might want to set.
+ * Other fixes:
+ - Add check when constructing DNS packet so that total length of labels
+ does not exceed 255 bytes
+ - Fix connection/reconnection state handling for DoT transport
+ - Fix event handling by initializing them directly when opening the
+ sockets, otherwise events could have been missed which would give
+ incorrect statistics
+ * Commits:
+ 61b5eac Tests with dumdumd
+ d71071c Tests with dumdumd
+ b42f92e DoH sending
+ 2fa40bb Net stats
+ f7f8692 DoH fixes
+ ea62b49 DoH concurrent streams
+ 91929f1 DoH reconnect
+ 17660e6 DoH fixes
+ 5276aa6 resperf buckets
+ 585860e Packages
+ 3ffc601 Fixes
+ 1570609 Man-page
+ 6bcadc7 README
+ f81adf1 Fixes
+ 1acd71f Code structure
+ 6c47876 Fixes
+ 2d379f4 Fixes
+ 4d5384b Fixes
+ cee93b3 Initial DNS-over-HTTPS support implementation
+ 4ff3ebc Events
+ 8b24bbf DoT state
+ 6a5b5ef Fix too long name
+ 71fa09f long opts
+* Mon May 31 2021 Jerry Lundström <lundstrom.jerry@gmail.com> 2.6.0-1
+- Release 2.6.0
+ * This release adds EDNS options parameter `-E` to `resperf` and a script
+ for generating EDNS Client Subnet options (see `contrib/ecs-gen`).
+ * Commits:
+ d29d880 ECS opt
+ 877f31e edns option
+* Thu Mar 25 2021 Jerry Lundström <lundstrom.jerry@gmail.com> 2.5.2-1
+- Release 2.5.2
+ * This release tweaks the reconnect code for TCP and DoT.
+ * For TCP, atomic operations are used to signal the need to reconnect
+ from the receiving thread to the sending, as the sending is the one in
+ charge of reconnecting.
+ This speeds up detection of connection lost which reduces the amount of
+ lost queries on a disconnect.
+ * This change does not affect DoT as much, as the SSL context shared
+ between the threads are protected by a mutex.
+ But a bug was found in `sendto()` for DoT that could drop a query if
+ the socket was busy sending.
+ * The connect and reconnect socket events has been split into connecting,
+ connected and reconnecting, reconnected. This is to report more correct
+ reconnect events when it comes to DoT, because the connection can be
+ lost while negotiating TLS.
+ * Lastly, additional tests has been added for the network code.
+ * Commits:
+ d9e5663 net test
+ 22f49df network tests
+ 8e5b56e reconnect
+* Mon Mar 22 2021 Jerry Lundström <lundstrom.jerry@gmail.com> 2.5.1-1
+- Release 2.5.1
+ * This release re-adds support for TYPEnnn and ANY in the datafile, this
+ was missed during the removal of the dependency on BINDs development
+ libraries in v2.4.0.
+ * Also note that with v2.5.0, this software now depends on Concurrency
+ Kit (ck) for atomic operations.
+ * Commits:
+ 924e6ea ANY
+ 0a444c0 TYPEnnn
+* Fri Mar 12 2021 Jerry Lundström <lundstrom.jerry@gmail.com> 2.5.0-1
+- Release 2.5.0
+ * This release adds re-connection support for TCP and DoT protocol,
+ new options to `resperf` and fixes a few bugs.
+ * `dnsperf` and `resperf` will now try to re-connect when they lose a TCP
+ or DoT connection, and with that comes a few new statistics metrics.
+ For `dnsperf`, if a connection oriented protocol is used, it will now
+ show the total number of re-connections made and the connection latency.
+ For `resperf` it also shows the total number of re-connections made and
+ the gnuplot data now contains the total number of connections made and
+ the connection latency for each interval.
+ Beside re-connection support, improvements have been made when it comes
+ to tracking socket readiness while connections are established which
+ should generate less warnings about "socket not ready".
+ * New `resperf` options:
+ - `-R`: Reopen the datafile if it runs out of data before the testing
+ is completed. This allows for long running tests on very small and
+ simple query datafile.
+ - `-F <fall_behind>`: Sets the maximum number of queries that can fall
+ behind being sent. `resperf` will stop when this many queries should
+ have been sent and it can be relative easy to hit if `-m <max_qps>`
+ is set too high.
+ The default is 1000 and setting it to zero (0) disables the check.
+ * Bugfixes:
+ - Fixed port handling for host/network format when setting client side
+ port with `-x`
+ - Fix support for quoted characters, `\000` and `\.`, in domain names,
+ this was lost when removing BIND's internal development libraries
+ - Fix issue in `dnsperf`, it would loop forever if no connection could
+ be established
+ - Fix potential buffer overrun in `resperf` when using response id
+ for `queries[]`
+ - DoT: Fix bug when sending from buffer
+ * Other changes:
+ - Always use `IPV6_V6ONLY` socket option for IPv6
+ - Add man-page on `-W` option added in v2.4.0
+ - Reformat man-pages
+ - `resperf`:
+ - Try and process more request each run to hopefully not hit max
+ outstanding so easy when high QPS
+ - Add default value to `-C` so it shows in help
+ * Commits:
+ 9308361 man-page format
+ 0e52fb4 man-page, opts, tuneups
+ e36211d stats
+ d9b9ba3 Response qid to index
+ 2b2c37e fixes, reconnection
+ acd31e5 dname quote
+ de8f049 net
+* Tue Feb 23 2021 Jerry Lundström <lundstrom.jerry@gmail.com> 2.4.2-1
+- Release 2.4.2
+ * This release fixes a few issues with reading of the datafile which
+ could lead to "ran out of data" errors.
+ * The problem was that reading from the datafile was done before finding
+ a socket to send it on, or socket readyness, and that lead to progressing
+ the queries without really doing anything.
+ Another issues that's been fixed was that if the read lines perfectly
+ aligned with the buffer, it would be treated like EOF and caused an exit.
+ * Commits:
+ 9937287 resperf TLS
+ 6736956 datafile
+ 55faec6 ran out of data
+* Tue Feb 09 2021 Jerry Lundström <lundstrom.jerry@gmail.com> 2.4.1-1
+- Release 2.4.1
+ * This release fixes an issue with the socket readiness function that
+ could cause a buffer overflow (`-T 10 -c 2000`) due to `select()` being
+ limited to check 1023 sockets. `poll()` is now used which has no limit.
+ * There has also been a few fixes to the contrib script `queryparse` that
+ has to do with python v2 and v3 compatibility and better exception
+ handling.
+ * Commits:
+ 24e5bee poll
+ 7dceca7 Handle only common exceptions
+ 5603294 Fix error on python3
+ 48fa517 TSIG
+* Wed Dec 09 2020 Jerry Lundström <lundstrom.jerry@gmail.com> 2.4.0-1
+- Release 2.4.0
+ * This release removes the dependency on BIND's internal development
+ libraries! This make building and packaging a lot easier and less
+ troublesome in the future.
+ * This software now depends only on OpenSSL (for TSIG feature) with an
+ optional depend on LDNS (for dynamic updates feature).
+ * New option:
+ - Transport mode option `-m`/`-M` now recognizes `dot` alongside `tls`
+ for encrypted DNS
+ - Added `-W` for outputting warnings and errors to stdout
+ * Other changes / bugfixes:
+ - Fix potential memory leak of query descriptions when using verbose
+ - Only use TLS v1.2 and above for DoT/TLS
+ - Add a lot of tests
+ - Add coverage testing
+ * Commits:
+ d17743b datafile
+ 434bbf2 Checks, coverage, log, test IPv6
+ 9fb305f Coverage
+ 123ebf1 DOT, TLS version, Sonarcloud
+ 26df0bd BIND dependency
+ ee660e7 Sonarcloud
+ c9ea0ab base64
+ 4e9be82 TSIG
+ 4275045 EDNS, https
+ 7c3f51c BIND dependency
+ 6e1be5d ISC dependencies
+ e36f19d Buffer
+ 485cdd2 ISC mem, tests
+ 663dc24 Namespace clash
+ 2c44987 dynamic updates, edns, headers
+ 5d109b2 Disable HMAC
+ 79cae93 datafile, query desc
+ 663d814 net
+ c867de6 isc_result_t
+ 651ee5d opt
+ 7d30804 isc_result_t
+ 58ad313 ISC linked list
+ 7b4da6d Info
+ 8079ebc Tests
+ e3fb685 Tests
+ 2bb603a Tests
+ 297b23b Test
+ c4e244b Test
+ 1caac35 Makefile
+ e9f2aaa Coverage
+ 27af853 Fix typo in configure.ac
+ 521faa6 Badges
+ 8fa2ec4 LGTM
+ 75c89e5 COPR
+* Fri May 15 2020 Jerry Lundström <lundstrom.jerry@gmail.com> 2.3.4-1
+- Release 2.3.4
+ * This release adds a workaround, thanks to patch from Petr Menšík, for
+ building on systems with BIND 9.16. Also improves error handling by
+ using thread-safe `strerror_r()` instead of `strerror()`.
+ * Commits:
+ 88c3ef4 strerror
+ 1917f67 openSUSE Tumbleweed
+ fd39641 AS_VAR_APPEND
+ aeeef74 bind 9.16
+ 07732cd BIND, libcrypto, clang format
+ 08146e3 Add crypto library to checks
+ e4307c2 Add checks to pass with BIND 9.16
+ 3f9aed3 Prepare check for bind 9.11/9.16 return types
+* Wed May 06 2020 Jerry Lundström <lundstrom.jerry@gmail.com> 2.3.3-1
+- Release 2.3.3
+ * This release changes the behavior of `dnsperf` and `resperf` when it
+ comes to TCP and TLS connections, and updates package building using
+ COPR (thanks to patch from Petr Menšík (Red Hat)).
+ * Connection reset or close are now treated as "try again" so that the
+ run is finished and not aborted. As SIGPIPE might be received on usage
+ of closed connections it's now blocked in `dnsperf` and handled as
+ a fatal action in `resperf`.
+ * Commits:
+ 62885ad SIGPIPE
+ 106c50e connection
+ 3ef0899 README
+ 61a3b1c COPR
+ 35efa27 COPR
+ 46b37a1 COPR
+ 5c126ae COPR
+ 1c51b76 Provide full URL in spec
+ 2a4dd0e Allow recreation of source archive
+ 931d6cc Do not require root for archive creation
+* Fri Aug 23 2019 Jerry Lundström <lundstrom.jerry@gmail.com> 2.3.2-1
+- Release 2.3.2
+ * This release fixes a buffer overflow when using TSIG and algorithms
+ with digests larger then SHA256, reported by Mukund Sivaraman. Also
+ fix build dependencies for `sqrt()`.
+ * Commits:
+ e54aa58 Digest
+ bca5d8d sqrt
+ d9eaa5b Package
+* Wed Jul 24 2019 Jerry Lundström <lundstrom.jerry@gmail.com> 2.3.1-1
+- Release 2.3.1
+ * After a report and additional confirming results the use of `poll()` in
+ the network receive code for TCP and TLS has been removed. This `poll()`
+ initially gave better results while testing in a docker container on
+ it's loopback interface but when on physical networks it reduced
+ performance to 1/12th, so it had to go.
+ * Thanks to Brian Wellington (Akamai/Nominum) for the initial report and
+ testing, and to Jan Hák (CZ.NIC) for testing and confirming the results.
+ * Bugfix:
+ - Fix check for having more DNS messages in the receive buffer for TCP
+ and TLS
+ * Commits:
+ 670db9c TCP/TLS receive
+ b8925b2 recvbuf have more
+* Wed Jul 17 2019 Jerry Lundström <lundstrom.jerry@gmail.com> 2.3.0-1
+- Release 2.3.0
+ * This release adds support for DNS over TCP and TLS which can be selected
+ by using the mode option for `dnsperf` and `resperf`. The default server
+ port used is now determined by the transport mode, udp/tcp port 53 and
+ tls port 853.
+ * Note that the mode option is different between the program because it was
+ already taken for `resperf`.
+ * `dnsperf` changes:
+ - Add `-m` for setting transport mode, `udp` (default), `tcp` or `tls`
+ - Add verbose messages about network readiness and congestion
+ * `resperf` changes:
+ - Add `-M` for setting transport mode, `udp` (default), `tcp` or `tls`
+ - Add `-v` for verbose mode to report about network readiness and
+ congestion
+ * Commits:
+ ffa49cf LGTM, SonarCloud
+ 4cd5441 TLS
+ 35624d1 TCP send, socket ready loop
+ fbf76aa TCP support
+ 5988b06 Funding
+* Mon Jan 28 2019 Jerry Lundström <lundstrom.jerry@gmail.com> 2.2.1-1
+- Release 2.2.1
+ * The commit pulled from a fork that used `inttypes.h`, instead of ISC
+ internal types, missed to remove the old conversion specifier.
+ This was reported and fixed by Vladimír Čunát.
+ * Commits:
+ 9534ce1 remove visible "u" characters after numbers
+* Mon Dec 03 2018 Jerry Lundström <lundstrom.jerry@gmail.com> 2.2.0-1
+- Release 2.2.0
+ * First release by DNS-OARC with a rework of the code to use autotools,
+ semantic versioning 2.0 and bugfixes pulled from other's forks.
+ * Bugfixes:
+ - Fix infinite loop in argument parsing
+ - Fix min/max latency summing for multithreaded runs
+ - Fix calculation of per_thread socket counts
+ - Fixes to queryparse
+ - Mark correctly end of file
+ - Support python3
+ - Stop looping on end of file undefinitely
+ - Fix compilation issues and work around missing `dns_fixedname_initname()`
+ - Clang `scan-build` fixes
+ * Other changes:
+ - add "configure --with-bind" option
+ - Handle bind library changes to HMAC (see #22) and other differences
+ between versions
+ - Workaround issue on FreeBSD (see #23)
+ - Use `snprintf()` and OpenBSD's `strlcat()`
+ - Add/update build dependencies for Debia, Ubuntu, CentOS, FreeBSD
+ and OpenBSD
+ * Commits:
+ ae9bc91 Clang format
+ b9bb085 CI, buildbot
+ b84e41b Autotools, README, changelog
+ a2e1732 License
+ 9dcb661 Remove $Id markers, Principal Author and Reviewed tags from the
+ full source tree
+ 0677bf0 Use dns_fixedname_initname() where possible
+ d8d4696 [master] add "configure --with-bind" option to dnsperf
+ b71a280 Add deb based distros dependencies
+ 439c614 Replace custom isc_boolean_t with C standard bool type
+ 407ae7c Replace custom isc_u?intNN_t types with C99 u?intNN_t types
+ c27afd4 Replace ISC_PRINT_QUADFORMAT with inttypes.h format constants
+ 6fdb2f7 Fix queryparse
+ 4909b78 README
+ 2782d50 README.md: Rectify link to software
+ e31ddf4 fix calculation of per_thread socket counts
+ 3bd7fb4 Fix min/max latency summing for multithreaded runs
+ 2207e27 Fix infinite loop in argument parsing.
+ 3bfe97a Include the github URL; remove the bug reports section.
+ 0cee04a Add note about bug reports.
+ 62c4b32 add .gitignore
+ c45f0be Initial import.
+ 149172b Initial commit
--- /dev/null
+sonar.coverage.exclusions=src/gen-qtype.c.py
+sonar.python.version=3
--- /dev/null
+# Copyright 2019-2026 OARC, Inc.
+# Copyright 2017-2018 Akamai Technologies
+# Copyright 2006-2016 Nominum, Inc.
+# All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+MAINTAINERCLEANFILES = $(srcdir)/Makefile.in $(srcdir)/config.h.in
+CLEANFILES = dnsperf.1 resperf.1 *.gcda *.gcno *.gcov \
+ */*.gcda */*.gcno */*.gcov
+
+SUBDIRS = test
+
+AM_CFLAGS = -I$(srcdir) \
+ -I$(top_srcdir) \
+ $(PTHREAD_CFLAGS) $(libssl_CFLAGS) $(libcrypto_CFLAGS) $(libldns_CFLAGS) \
+ $(json_c_CFLAGS)
+
+EXTRA_DIST = dnsperf.1.in resperf-report resperf.1.in
+
+bin_PROGRAMS = dnsperf resperf
+dist_bin_SCRIPTS = resperf-report
+
+_libperf_sources = datafile.c dns.c log.c net.c opt.c os.c strerror.c qtype.c \
+ edns.c tsig.c net_udp.c net_tcp.c net_dot.c net_doh.c ext/parse_uri.c
+
+_libperf_headers = datafile.h dns.h log.h net.h opt.h os.h util.h strerror.h \
+ list.h result.h buffer.h qtype.h edns.h tsig.h ext/parse_uri.h
+
+if HAVE_STDATOMIC
+_libperf_sources += ext/hg64.c
+_libperf_headers += ext/hg64.h
+endif
+
+dnsperf_SOURCES = $(_libperf_sources) dnsperf.c
+dist_dnsperf_SOURCES = $(_libperf_headers)
+dnsperf_LDADD = $(PTHREAD_LIBS) $(libssl_LIBS) $(libcrypto_LIBS) \
+ $(libldns_LIBS) $(libnghttp2_LIBS) $(json_c_LIBS)
+
+resperf_SOURCES = $(_libperf_sources) resperf.c
+dist_resperf_SOURCES = $(_libperf_headers)
+resperf_LDADD = $(PTHREAD_LIBS) $(libssl_LIBS) $(libcrypto_LIBS) \
+ $(libldns_LIBS) $(libnghttp2_LIBS) $(json_c_LIBS)
+
+man1_MANS = dnsperf.1 resperf.1
+
+dnsperf.1: dnsperf.1.in Makefile
+ sed -e 's,[@]PACKAGE_VERSION[@],$(PACKAGE_VERSION),g' \
+-e 's,[@]PACKAGE_URL[@],$(PACKAGE_URL),g' \
+-e 's,[@]PACKAGE_BUGREPORT[@],$(PACKAGE_BUGREPORT),g' \
+< $(srcdir)/dnsperf.1.in > dnsperf.1
+
+resperf.1: resperf.1.in Makefile
+ sed -e 's,[@]PACKAGE_VERSION[@],$(PACKAGE_VERSION),g' \
+-e 's,[@]PACKAGE_URL[@],$(PACKAGE_URL),g' \
+-e 's,[@]PACKAGE_BUGREPORT[@],$(PACKAGE_BUGREPORT),g' \
+< $(srcdir)/resperf.1.in > resperf.1
+
+if ENABLE_GCOV
+gcov-local:
+ for src in $(_libperf_sources) dnsperf.c resperf.c; do \
+ gcov -l -r -s "$(srcdir)" "$$src"; \
+ done
+endif
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef PERF_BUFFER_H
+#define PERF_BUFFER_H 1
+
+#include <stddef.h>
+
+typedef struct perf_region {
+ void* base;
+ size_t length;
+} perf_region_t;
+
+typedef struct perf_buffer {
+ void* base;
+ size_t length, used, current, active;
+} perf_buffer_t;
+
+#include <assert.h>
+
+#define perf_buffer_init(b, _base, _length) \
+ { \
+ (b)->base = _base; \
+ (b)->length = _length; \
+ (b)->used = 0; \
+ (b)->current = 0; \
+ (b)->active = 0; \
+ }
+
+#define perf_buffer_add(b, n) \
+ { \
+ assert((b)->used + n <= (b)->length); \
+ (b)->used += (n); \
+ }
+
+#define perf_buffer_length(b) ((b)->length)
+
+#define perf_buffer_availablelength(b) ((b)->length - (b)->used)
+
+#define perf_buffer_base(b) ((b)->base)
+
+#define perf_buffer_clear(b) \
+ { \
+ (b)->used = 0; \
+ (b)->current = 0; \
+ (b)->active = 0; \
+ }
+
+#define perf_buffer_putmem(b, base, length) \
+ { \
+ assert(perf_buffer_availablelength(b) >= length); \
+ memcpy(perf_buffer_used(b), base, length); \
+ perf_buffer_add(b, length); \
+ }
+
+#define perf_buffer_putuint8(b, _val) \
+ { \
+ unsigned char* _cp; \
+ uint8_t _val2 = (_val); \
+ assert(perf_buffer_availablelength(b) >= 1U); \
+ _cp = perf_buffer_used(b); \
+ (b)->used += 1U; \
+ _cp[0] = _val2; \
+ }
+
+#define perf_buffer_putuint16(b, _val) \
+ { \
+ unsigned char* _cp; \
+ uint16_t _val2 = (_val); \
+ assert(perf_buffer_availablelength(b) >= 2U); \
+ _cp = perf_buffer_used(b); \
+ (b)->used += 2U; \
+ _cp[0] = _val2 >> 8; \
+ _cp[1] = _val2; \
+ }
+
+#define perf_buffer_putuint32(b, _val) \
+ { \
+ unsigned char* _cp; \
+ uint32_t _val2 = (_val); \
+ assert(perf_buffer_availablelength(b) >= 4U); \
+ _cp = perf_buffer_used(b); \
+ (b)->used += 4U; \
+ _cp[0] = _val2 >> 24; \
+ _cp[1] = _val2 >> 16; \
+ _cp[2] = _val2 >> 8; \
+ _cp[3] = _val2; \
+ }
+
+#define perf_buffer_copyregion(b, r) perf_buffer_putmem(b, (r)->base, (r)->length)
+
+#define perf_buffer_used(b) ((void*)((unsigned char*)(b)->base + (b)->used))
+#define perf_buffer_usedlength(b) ((b)->used)
+#define perf_buffer_usedregion(b, r) \
+ { \
+ (r)->base = (b)->base; \
+ (r)->length = (b)->used; \
+ }
+
+#endif
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "config.h"
+
+#include "datafile.h"
+
+#include "log.h"
+#include "os.h"
+#include "util.h"
+
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <assert.h>
+
+static perf_result_t read_one_blob(perf_datafile_t* dfile, perf_buffer_t* wire);
+static perf_result_t read_one_line(perf_datafile_t* dfile, perf_buffer_t* lines);
+
+perf_datafile_t* perf_datafile_open(const char* filename, perf_input_format_t format)
+{
+ perf_datafile_t* dfile;
+ struct stat buf;
+
+ dfile = calloc(1, sizeof(perf_datafile_t));
+ if (!dfile) {
+ perf_log_fatal("out of memory");
+ return 0; // fix clang scan-build
+ }
+
+ PERF_MUTEX_INIT(&dfile->lock);
+ dfile->pipe_fd = -1;
+ dfile->is_file = false;
+ dfile->size = 0;
+ dfile->cached = false;
+ dfile->maxruns = 1;
+ dfile->nruns = 0;
+ dfile->read_any = false;
+ if (!filename) {
+ dfile->fd = STDIN_FILENO;
+ } else {
+ dfile->fd = open(filename, O_RDONLY);
+ if (dfile->fd < 0)
+ perf_log_fatal("unable to open file: %s", filename);
+ if (fstat(dfile->fd, &buf) == 0 && S_ISREG(buf.st_mode)) {
+ dfile->is_file = true;
+ dfile->size = buf.st_size;
+ }
+ }
+ dfile->format = format;
+ switch (format) {
+ case input_format_text_query:
+ case input_format_text_update:
+ dfile->readfunc = read_one_line;
+ break;
+ case input_format_tcp_wire_format:
+ dfile->readfunc = read_one_blob;
+ break;
+ default:
+ perf_log_fatal("invalid datafile format");
+ };
+
+ return dfile;
+}
+
+void perf_datafile_close(perf_datafile_t** dfilep)
+{
+ perf_datafile_t* dfile;
+
+ assert(dfilep);
+ assert(*dfilep);
+
+ dfile = *dfilep;
+ *dfilep = 0;
+
+ if (dfile->fd >= 0 && dfile->fd != STDIN_FILENO) {
+ close(dfile->fd);
+ }
+ PERF_MUTEX_DESTROY(&dfile->lock);
+ free(dfile);
+}
+
+void perf_datafile_setpipefd(perf_datafile_t* dfile, int pipe_fd)
+{
+ dfile->pipe_fd = pipe_fd;
+}
+
+void perf_datafile_setmaxruns(perf_datafile_t* dfile, unsigned int maxruns)
+{
+ dfile->maxruns = maxruns;
+}
+
+static void reopen_file(perf_datafile_t* dfile)
+{
+ if (dfile->cached) {
+ dfile->at = 0;
+ } else {
+ if (lseek(dfile->fd, 0L, SEEK_SET) < 0) {
+ perf_log_fatal("cannot reread input");
+ }
+ dfile->at = 0;
+ dfile->have = 0;
+ dfile->databuf[0] = 0;
+ }
+}
+
+static perf_result_t read_more(perf_datafile_t* dfile)
+{
+ ssize_t n;
+ perf_result_t result;
+ struct perf_net_socket sock = { .mode = sock_file, .fd = dfile->fd };
+
+ if (!dfile->is_file && dfile->pipe_fd >= 0) {
+ result = perf_os_waituntilreadable(&sock, dfile->pipe_fd, -1);
+ if (result != PERF_R_SUCCESS)
+ return (result);
+ }
+
+ if (dfile->at && dfile->at < dfile->have) {
+ memmove(dfile->databuf, &dfile->databuf[dfile->at], dfile->have - dfile->at);
+ dfile->have -= dfile->at;
+ dfile->at = 0;
+ } else if (dfile->at == dfile->have) {
+ dfile->have = 0;
+ dfile->at = 0;
+ }
+
+ /* leave space for \0 string termination at the end */
+ n = read(dfile->fd, &dfile->databuf[dfile->have], sizeof(dfile->databuf) - dfile->have - 1);
+ if (n < 0) {
+ return (PERF_R_FAILURE);
+ } else if (n == 0) {
+ return (PERF_R_EOF);
+ }
+
+ dfile->have += n;
+
+ if (dfile->is_file && dfile->have == dfile->size) {
+ dfile->cached = true;
+ }
+
+ return (PERF_R_SUCCESS);
+}
+
+/* Binary format: (<uint16_t> for blob length, <bytes>), repeat
+ * Outputs single packet _without_ the length preambule. */
+static perf_result_t read_one_blob(perf_datafile_t* dfile, perf_buffer_t* wire)
+{
+ perf_result_t result;
+ uint16_t packet_size; /* 2-byte preambule like in the TCP stream */
+
+ while (true) {
+ if ((dfile->have - dfile->at) < sizeof(packet_size)) {
+ /* we don't have complete preambule yet */
+ if (dfile->cached) {
+ if ((dfile->have - dfile->at) == 0) {
+ return PERF_R_EOF;
+ } else {
+ return PERF_R_INVALIDFILE;
+ }
+ }
+ result = read_more(dfile);
+ if (result != PERF_R_SUCCESS) {
+ if (result == PERF_R_EOF && (dfile->have - dfile->at) != 0) {
+ /* incomplete preambule at the end of file */
+ result = PERF_R_INVALIDFILE;
+ }
+ return (result);
+ }
+ continue;
+ }
+ memcpy(&packet_size, &dfile->databuf[dfile->at], sizeof(uint16_t));
+ packet_size = ntohs(packet_size);
+ break;
+ }
+ while (true) {
+ if (sizeof(packet_size) + packet_size > (dfile->have - dfile->at)) {
+ if (dfile->cached) {
+ return PERF_R_INVALIDFILE;
+ }
+ result = read_more(dfile);
+ if (result != PERF_R_SUCCESS) {
+ if (result == PERF_R_EOF) {
+ /* incomplete blob at the end of file */
+ result = PERF_R_INVALIDFILE;
+ }
+ return (result);
+ }
+ continue;
+ }
+ break;
+ }
+
+ perf_buffer_putmem(wire, ((unsigned char*)&dfile->databuf[dfile->at]) + sizeof(packet_size), packet_size);
+ dfile->at += sizeof(packet_size) + packet_size;
+ return (PERF_R_SUCCESS);
+}
+
+/* String in output buffer is not \0 terminated, check length in dfile->have */
+static perf_result_t read_one_line(perf_datafile_t* dfile, perf_buffer_t* lines)
+{
+ const char* cur;
+ size_t length, curlen, nrem;
+ perf_result_t result;
+
+ while (true) {
+ /* Get the current line */
+ cur = &dfile->databuf[dfile->at];
+ curlen = strcspn(cur, "\n");
+
+ /*
+ * If the current line contains the rest of the buffer,
+ * we need to read more (unless the full file is cached).
+ */
+ nrem = dfile->have - dfile->at;
+ if (curlen == nrem) {
+ if (!dfile->cached) {
+ result = read_more(dfile);
+ /* line terminator for text input */
+ dfile->databuf[dfile->have] = 0;
+ if (result != PERF_R_SUCCESS)
+ return (result);
+ }
+ if (dfile->have - dfile->at == 0) {
+ return (PERF_R_EOF);
+ }
+ if (dfile->have - dfile->at > nrem)
+ continue;
+ }
+
+ /* We now have a line. Advance the buffer past it. */
+ dfile->at += curlen;
+ if (dfile->at < dfile->have) {
+ dfile->at += 1;
+ }
+
+ /* If the line is empty or a comment, we need to try again. */
+ if (curlen > 0 && cur[0] != ';')
+ break;
+ }
+
+ length = perf_buffer_availablelength(lines);
+ if (curlen > length - 1)
+ curlen = length - 1;
+ perf_buffer_putmem(lines, (unsigned char*)cur, curlen);
+ perf_buffer_putuint8(lines, 0);
+
+ return (PERF_R_SUCCESS);
+}
+
+perf_result_t perf_datafile_next(perf_datafile_t* dfile, perf_buffer_t* lines)
+{
+ const char* current;
+ perf_result_t result;
+
+ PERF_LOCK(&dfile->lock);
+
+ if (dfile->maxruns > 0 && dfile->maxruns == dfile->nruns) {
+ result = PERF_R_EOF;
+ goto done;
+ }
+
+ result = dfile->readfunc(dfile, lines);
+ if (result == PERF_R_EOF) {
+ if (!dfile->read_any) {
+ result = PERF_R_INVALIDFILE;
+ goto done;
+ }
+ dfile->nruns++;
+ if (dfile->maxruns != dfile->nruns) {
+ reopen_file(dfile);
+ result = dfile->readfunc(dfile, lines);
+ }
+ }
+ if (result != PERF_R_SUCCESS) {
+ goto done;
+ }
+ dfile->read_any = true;
+
+ if (dfile->format == input_format_text_update) {
+ while (true) {
+ current = perf_buffer_used(lines);
+ result = dfile->readfunc(dfile, lines);
+ if (result == PERF_R_EOF) {
+ dfile->nruns++;
+ if (dfile->maxruns != dfile->nruns) {
+ reopen_file(dfile);
+ }
+ }
+ if (result != PERF_R_SUCCESS || strcasecmp(current, "send") == 0)
+ break;
+ }
+ }
+
+ result = PERF_R_SUCCESS;
+done:
+ PERF_UNLOCK(&dfile->lock);
+ return (result);
+}
+
+unsigned int perf_datafile_nruns(const perf_datafile_t* dfile)
+{
+ return dfile->nruns;
+}
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "result.h"
+#include "buffer.h"
+
+#ifndef PERF_DATAFILE_H
+#define PERF_DATAFILE_H 1
+
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+
+typedef enum {
+ input_format_text_query,
+ input_format_text_update,
+ input_format_tcp_wire_format
+} perf_input_format_t;
+
+typedef struct perf_datafile perf_datafile_t;
+struct perf_datafile {
+ pthread_mutex_t lock;
+ int pipe_fd;
+ int fd;
+ bool is_file;
+ size_t size, at, have;
+ bool cached;
+ char databuf[(64 * 1024) + sizeof(uint16_t)]; /* pad for null-terminated string or TCP wire length */
+ unsigned int maxruns;
+ unsigned int nruns;
+ bool read_any;
+
+ perf_input_format_t format;
+ perf_result_t (*readfunc)(perf_datafile_t* dfile, perf_buffer_t* lines);
+};
+
+perf_datafile_t* perf_datafile_open(const char* filename, perf_input_format_t format);
+
+void perf_datafile_close(perf_datafile_t** dfilep);
+void perf_datafile_setmaxruns(perf_datafile_t* dfile, unsigned int maxruns);
+void perf_datafile_setpipefd(perf_datafile_t* dfile, int pipe_fd);
+
+perf_result_t perf_datafile_next(perf_datafile_t* dfile, perf_buffer_t* lines);
+
+unsigned int perf_datafile_nruns(const perf_datafile_t* dfile);
+
+#endif
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "config.h"
+
+#include "dns.h"
+
+#include "log.h"
+#include "opt.h"
+#include "qtype.h"
+
+#include <ctype.h>
+#include <time.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <limits.h>
+
+#ifdef HAVE_LDNS
+#include <ldns/ldns.h>
+#endif
+
+#define WHITESPACE " \t\n"
+
+#define MAX_RDATA_LENGTH 65535
+#define EDNSLEN 11
+
+const char* perf_dns_rcode_strings[] = {
+ "NOERROR", "FORMERR", "SERVFAIL", "NXDOMAIN",
+ "NOTIMP", "REFUSED", "YXDOMAIN", "YXRRSET",
+ "NXRRSET", "NOTAUTH", "NOTZONE", "rcode11",
+ "rcode12", "rcode13", "rcode14", "rcode15"
+};
+
+perf_result_t perf_dname_fromstring(const char* str, size_t len, perf_buffer_t* target)
+{
+ size_t label_len, at;
+ ssize_t max = 255;
+ const char* orig_str = str;
+ bool is_quoted;
+
+ if (perf_buffer_availablelength(target) < len) {
+ return PERF_R_NOSPACE;
+ }
+
+ while (len) {
+ is_quoted = false;
+ for (label_len = 0, at = 0; at < len;) {
+ if (*(str + at) == '\\') {
+ is_quoted = true;
+ at++;
+ if (at >= len)
+ return PERF_R_FAILURE;
+ if (*(str + at) >= '0' && *(str + at) <= '2') {
+ at++;
+ if (at >= len)
+ return PERF_R_FAILURE;
+ if (*(str + at) < '0' || *(str + at) > '9')
+ return PERF_R_FAILURE;
+ at++;
+ if (at >= len)
+ return PERF_R_FAILURE;
+ if (*(str + at) < '0' || *(str + at) > '9')
+ return PERF_R_FAILURE;
+ }
+ } else if (*(str + at) == '.') {
+ break;
+ }
+ label_len++;
+ at++;
+ }
+ if (!label_len) {
+ // Just a dot
+ if (len > 1) {
+ // a dot but with labels after it
+ return PERF_R_FAILURE;
+ } else if (str != orig_str) {
+ // a dot but with labels before it
+ return PERF_R_FAILURE;
+ }
+ perf_buffer_putuint8(target, 0);
+ break;
+ }
+ if (label_len > 63) {
+ return PERF_R_FAILURE;
+ }
+ max -= label_len + 1;
+ if (max < 0) {
+ return PERF_R_FAILURE;
+ }
+ perf_buffer_putuint8(target, label_len);
+ if (is_quoted) {
+ for (at = 0; at < len; at++) {
+ if (*(str + at) == '\\') {
+ at++;
+ if (*(str + at) >= '0' && *(str + at) <= '9') {
+ char b[4];
+ long v;
+ memcpy(b, str + at, 3);
+ at += 2;
+ b[3] = 0;
+ v = strtol(b, 0, 10);
+ if (v < 0 || v > 255)
+ return PERF_R_FAILURE;
+ perf_buffer_putuint8(target, (uint8_t)v);
+ continue;
+ }
+ } else if (*(str + at) == '.') {
+ break;
+ }
+ perf_buffer_putmem(target, str + at, 1);
+ }
+ str += at;
+ len -= at;
+ } else {
+ perf_buffer_putmem(target, str, label_len);
+ str += label_len;
+ len -= label_len;
+ }
+ if (len < 2) {
+ // Last label/dot
+ perf_buffer_putuint8(target, 0);
+ break;
+ }
+ // advance past dot
+ str++;
+ len--;
+ }
+
+ return PERF_R_SUCCESS;
+}
+
+perf_result_t perf_qtype_fromstring(const char* str, size_t len, perf_buffer_t* target)
+{
+ const perf_qtype_t* q = qtype_table;
+
+ if (len > 4 && !strncasecmp(str, "TYPE", 4)) {
+ char* endptr = 0;
+ unsigned long int u = strtoul(str + 4, &endptr, 10);
+ if (endptr != str + len || u == ULONG_MAX || u > 65535) {
+ return PERF_R_FAILURE;
+ }
+
+ perf_buffer_putuint16(target, u);
+ return PERF_R_SUCCESS;
+ }
+
+ while (q->type) {
+ if (!strncasecmp(q->type, str, len)) {
+ perf_buffer_putuint16(target, q->value);
+ return PERF_R_SUCCESS;
+ }
+ q++;
+ }
+
+ return PERF_R_FAILURE;
+}
+
+static perf_result_t build_query(const perf_region_t* line, perf_buffer_t* msg)
+{
+ char * domain_str, *qtype_str;
+ size_t domain_len, qtype_len;
+ perf_result_t result;
+
+ domain_str = line->base;
+ domain_len = strcspn(line->base, WHITESPACE);
+
+ if (!domain_len) {
+ perf_log_warning("invalid query input format: %s", (char*)line->base);
+ return PERF_R_FAILURE;
+ }
+
+ qtype_str = line->base + domain_len;
+ while (isspace(*qtype_str))
+ qtype_str++;
+ qtype_len = strcspn(qtype_str, WHITESPACE);
+
+ /* Create the question section */
+ result = perf_dname_fromstring(domain_str, domain_len, msg);
+ if (result != PERF_R_SUCCESS) {
+ perf_log_warning("invalid domain name (or out of space): %.*s", (int)domain_len, domain_str);
+ return result;
+ }
+
+ if (!qtype_len) {
+ perf_log_warning("invalid query input format: %s", (char*)line->base);
+ return PERF_R_FAILURE;
+ }
+
+ result = perf_qtype_fromstring(qtype_str, qtype_len, msg);
+ if (result != PERF_R_SUCCESS) {
+ perf_log_warning("invalid qtype: %.*s", (int)qtype_len, qtype_str);
+ return result;
+ }
+
+ perf_buffer_putuint16(msg, 1); // class IN
+
+ return PERF_R_SUCCESS;
+}
+
+#ifdef HAVE_LDNS
+static bool token_equals(const perf_region_t* token, const char* str)
+{
+ return (strlen(str) == token->length && strncasecmp(str, token->base, token->length) == 0);
+}
+
+/*
+ * Reads one line containing an individual update for a dynamic update message.
+ */
+static perf_result_t
+read_update_line(char* str, const ldns_rdf* origin,
+ bool want_ttl, bool need_type, bool want_rdata, bool need_rdata,
+ ldns_rr** rr, const char** errstr)
+{
+ char tmp[256], *str2;
+ size_t len;
+
+ while (isspace(*str & 0xff))
+ str++;
+ str2 = str;
+
+ /*
+ * Read the owner name
+ */
+ len = strcspn(str, WHITESPACE);
+ if (len > sizeof(tmp) - 1) {
+ *errstr = "domain name too large";
+ return PERF_R_NOSPACE;
+ }
+ memcpy(tmp, str, len);
+ tmp[len] = 0;
+
+ ldns_rdf* owner;
+ if (!(owner = ldns_dname_new_frm_str(tmp))) {
+ *errstr = "invalid name or out of memory";
+ return PERF_R_FAILURE;
+ }
+ ldns_rr_set_owner(*rr, owner);
+ if (!ldns_dname_str_absolute(tmp) && origin) {
+ if (ldns_dname_cat(ldns_rr_owner(*rr), origin) != LDNS_STATUS_OK) {
+ return PERF_R_FAILURE;
+ }
+ }
+
+ str += len;
+ while (isspace(*str & 0xff))
+ str++;
+
+ /*
+ * Read the ttl
+ */
+ if (want_ttl) {
+ len = strcspn(str, WHITESPACE);
+ if (len > sizeof(tmp) - 1) {
+ *errstr = "TTL string too large";
+ return PERF_R_NOSPACE;
+ }
+ memcpy(tmp, str, len);
+ tmp[len] = 0;
+
+ char* endptr = 0;
+ unsigned long int u = strtoul(tmp, &endptr, 10);
+ if (*endptr || u == ULONG_MAX) {
+ *errstr = "TTL invalid";
+ return PERF_R_INVALIDUPDATE;
+ }
+
+ ldns_rr_set_ttl(*rr, u);
+
+ str += len;
+ while (isspace(*str & 0xff))
+ str++;
+ }
+
+ /*
+ * Read the type
+ */
+ len = strcspn(str, WHITESPACE);
+ if (!len) {
+ if (!need_type)
+ return PERF_R_SUCCESS;
+
+ *errstr = "TYPE required";
+ return PERF_R_INVALIDUPDATE;
+ }
+ if (len > sizeof(tmp) - 1) {
+ *errstr = "TYPE string too large";
+ return PERF_R_NOSPACE;
+ }
+ memcpy(tmp, str, len);
+ tmp[len] = 0;
+
+ ldns_rr_type type = ldns_get_rr_type_by_name(tmp);
+ if (!type) {
+ *errstr = "TYPE invalid";
+ return PERF_R_INVALIDUPDATE;
+ }
+ ldns_rr_set_type(*rr, type);
+
+ str += len;
+ while (isspace(*str & 0xff))
+ str++;
+
+ if (!want_rdata)
+ return PERF_R_SUCCESS;
+
+ /*
+ * Read the rdata
+ */
+ if (*str == 0) {
+ if (!need_rdata)
+ return PERF_R_SUCCESS;
+
+ *errstr = "RDATA required";
+ return PERF_R_INVALIDUPDATE;
+ }
+
+ // Need to recreate ldns_rr because there is no new_frm_str function to
+ // correctly parse RDATA (quotes etc) for a RDF
+ ldns_rr* rr2 = 0;
+ if (ldns_rr_new_frm_str(&rr2, str2, 0, origin, 0) != LDNS_STATUS_OK) {
+ *errstr = "invalid RDATA or out of memory";
+ return PERF_R_INVALIDUPDATE;
+ }
+
+ // Force set TTL since if its missing in the input it will get the default
+ // 3600 and not 0 as it should
+ ldns_rr_set_ttl(rr2, ldns_rr_ttl(*rr));
+
+ ldns_rr_free(*rr);
+ *rr = rr2;
+
+ return PERF_R_SUCCESS;
+}
+
+static void compression_free(ldns_rbnode_t* node, void* arg)
+{
+ (void)arg;
+ ldns_rdf_deep_free((ldns_rdf*)node->key);
+ LDNS_FREE(node);
+}
+
+/*
+ * Reads a complete dynamic update message and sends it.
+ */
+static perf_result_t build_update(const perf_region_t* record, perf_buffer_t* msg)
+{
+ perf_region_t input, token;
+ char * msgbase, *str;
+ bool is_update;
+ int updates = 0;
+ int prereqs = 0;
+ perf_result_t result = PERF_R_FAILURE;
+ ldns_rdf* origin = 0;
+ ldns_rr* rr = 0;
+ ldns_buffer* lmsg = 0;
+ ldns_rbtree_t compression;
+ const char* errstr;
+
+ input = *record;
+ msgbase = perf_buffer_base(msg);
+ ldns_rbtree_init(&compression, ldns_dname_compare_v);
+
+ // Fill LDNS buffer with current message (DNS headers)
+ if (!(lmsg = ldns_buffer_new(perf_buffer_length(msg)))) {
+ perf_log_fatal("unable to create LDNS buffer for DNS message");
+ goto done; // for scan-build / sonarcloud
+ }
+ ldns_buffer_write(lmsg, perf_buffer_base(msg), perf_buffer_usedlength(msg));
+
+ if (!(origin = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_DNAME, input.base))) {
+ perf_log_warning("Unable to parse domain name %s", (char*)input.base);
+ goto done;
+ }
+ if (ldns_dname2buffer_wire_compress(lmsg, origin, &compression) != LDNS_STATUS_OK) {
+ perf_log_warning("Unable to write domain name %s to wire format", (char*)input.base);
+ goto done;
+ }
+
+ ldns_buffer_write_u16(lmsg, 6); // SOA
+ ldns_buffer_write_u16(lmsg, 1); // IN
+
+ while (true) {
+ input.base += strlen(input.base) + 1;
+ if (input.base >= record->base + record->length) {
+ perf_log_warning("incomplete update: %s", (char*)record->base);
+ result = PERF_R_FAILURE;
+ goto done;
+ }
+
+ is_update = false;
+ token.base = input.base;
+ token.length = strcspn(token.base, WHITESPACE);
+ str = input.base + token.length;
+ errstr = 0;
+ if (token_equals(&token, "send")) {
+ break;
+ }
+
+ rr = ldns_rr_new();
+ ldns_rr_set_ttl(rr, 0);
+ ldns_rr_set_type(rr, LDNS_RR_TYPE_ANY);
+ ldns_rr_set_class(rr, LDNS_RR_CLASS_IN);
+
+ if (token_equals(&token, "add")) {
+ result = read_update_line(str, origin, true, true, true, true, &rr, &errstr);
+ ldns_rr_set_class(rr, LDNS_RR_CLASS_IN);
+ is_update = true;
+ } else if (token_equals(&token, "delete")) {
+ result = read_update_line(str, origin, false, false, true, false, &rr, &errstr);
+ if (ldns_rr_rd_count(rr)) {
+ ldns_rr_set_class(rr, LDNS_RR_CLASS_NONE);
+ } else {
+ ldns_rr_set_class(rr, LDNS_RR_CLASS_ANY);
+ }
+ is_update = true;
+ } else if (token_equals(&token, "require")) {
+ result = read_update_line(str, origin, false, false, true, false, &rr, &errstr);
+ if (ldns_rr_rd_count(rr)) {
+ ldns_rr_set_class(rr, LDNS_RR_CLASS_IN);
+ } else {
+ ldns_rr_set_class(rr, LDNS_RR_CLASS_ANY);
+ }
+ is_update = false;
+ } else if (token_equals(&token, "prohibit")) {
+ result = read_update_line(str, origin, false, false, false, false, &rr, &errstr);
+ ldns_rr_set_class(rr, LDNS_RR_CLASS_NONE);
+ is_update = false;
+ } else {
+ perf_log_warning("invalid update command: %s", (char*)input.base);
+ result = PERF_R_FAILURE;
+ }
+
+ if (result != PERF_R_SUCCESS) {
+ if (errstr) {
+ perf_log_warning("invalid update command, %s: %s", errstr, (char*)input.base);
+ } else if (result == PERF_R_INVALIDUPDATE) {
+ perf_log_warning("invalid update command: %s", (char*)input.base);
+ } else {
+ perf_log_warning("error processing update command: %s", (char*)input.base);
+ }
+ ldns_rr_free(rr);
+ goto done;
+ }
+
+ if (!is_update && updates > 0) {
+ perf_log_warning("prereqs must precede updates");
+ result = PERF_R_FAILURE;
+ ldns_rr_free(rr);
+ goto done;
+ }
+
+ if (ldns_rr2buffer_wire_compress(lmsg, rr, LDNS_SECTION_ANSWER, &compression) != LDNS_STATUS_OK) {
+ perf_log_warning("Unable to write update message to wire format");
+ ldns_rr_free(rr);
+ goto done;
+ }
+ ldns_rr_free(rr);
+
+ if (is_update)
+ updates++;
+ else
+ prereqs++;
+ }
+
+ if (ldns_buffer_position(lmsg) - perf_buffer_usedlength(msg) > perf_buffer_availablelength(msg)) {
+ perf_log_warning("out of space in message buffer");
+ result = PERF_R_NOSPACE;
+ goto done;
+ }
+ uint8_t* p = ldns_buffer_begin(lmsg) + perf_buffer_usedlength(msg);
+ perf_buffer_putmem(msg, p, ldns_buffer_position(lmsg) - perf_buffer_usedlength(msg));
+
+ msgbase[7] = prereqs; /* ANCOUNT = number of prereqs */
+ msgbase[9] = updates; /* AUCOUNT = number of updates */
+
+ result = PERF_R_SUCCESS;
+
+done:
+ ldns_buffer_free(lmsg);
+ ldns_rdf_deep_free(origin);
+ ldns_traverse_postorder(&compression, compression_free, 0);
+
+ return result;
+}
+#endif
+
+perf_result_t perf_dns_buildrequest(const perf_region_t* record, uint16_t qid,
+ bool edns, bool dnssec, bool is_update,
+ perf_tsigkey_t* tsigkey, perf_ednsoption_t* edns_option,
+ perf_buffer_t* msg)
+{
+ unsigned int flags;
+ perf_result_t result;
+
+ if (is_update)
+ flags = 5 << 11; // opcode UPDATE
+ else
+ flags = 0x0100U; // flag RD
+
+ /* Create the DNS packet header */
+ perf_buffer_putuint16(msg, qid);
+ perf_buffer_putuint16(msg, flags); /* flags */
+ perf_buffer_putuint16(msg, 1); /* qdcount */
+ perf_buffer_putuint16(msg, 0); /* ancount */
+ perf_buffer_putuint16(msg, 0); /* aucount */
+ perf_buffer_putuint16(msg, 0); /* arcount */
+
+ if (is_update) {
+#ifdef HAVE_LDNS
+ result = build_update(record, msg);
+#else
+ result = PERF_R_FAILURE;
+#endif
+ } else {
+ result = build_query(record, msg);
+ }
+
+ if (result == PERF_R_SUCCESS && edns) {
+ result = perf_add_edns(msg, dnssec, edns_option);
+ }
+
+ if (result == PERF_R_SUCCESS && tsigkey) {
+ result = perf_add_tsig(msg, tsigkey);
+ }
+
+ return result;
+}
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "result.h"
+#include "buffer.h"
+#include "edns.h"
+#include "tsig.h"
+
+#ifndef PERF_DNS_H
+#define PERF_DNS_H 1
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#define MAX_UDP_PACKET 512
+
+extern const char* perf_dns_rcode_strings[];
+
+perf_result_t perf_dname_fromstring(const char* str, size_t len, perf_buffer_t* target);
+perf_result_t perf_qtype_fromstring(const char* str, size_t len, perf_buffer_t* target);
+
+perf_result_t perf_dns_buildrequest(const perf_region_t* record, uint16_t qid,
+ bool edns, bool dnssec, bool is_update,
+ perf_tsigkey_t* tsigkey, perf_ednsoption_t* edns_option,
+ perf_buffer_t* msg);
+
+#endif
--- /dev/null
+.\" Copyright 2019-2026 OARC, Inc.
+.\" Copyright 2017-2018 Akamai Technologies
+.\" Copyright 2006-2016 Nominum, Inc.
+.\" All rights reserved.
+.\"
+.\" Licensed under the Apache License, Version 2.0 (the "License");
+.\" you may not use this file except in compliance with the License.
+.\" You may obtain a copy of the License at
+.\"
+.\" http://www.apache.org/licenses/LICENSE-2.0
+.\"
+.\" Unless required by applicable law or agreed to in writing, software
+.\" distributed under the License is distributed on an "AS IS" BASIS,
+.\" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+.\" See the License for the specific language governing permissions and
+.\" limitations under the License.
+.TH dnsperf 1 "@PACKAGE_VERSION@" "dnsperf"
+.SH NAME
+dnsperf \- test the performance of a DNS server
+.SH SYNOPSIS
+.hy 0
+.ad l
+\fBdnsperf\fR\ [\fB\-a\ \fIlocal_addr\fR]
+[\fB\-b\ \fIbufsize\fR]
+[\fB\-B\fR]
+[\fB\-c\ \fIclients\fR]
+[\fB\-d\ \fIdatafile\fR]
+[\fB\-D\fR]
+[\fB\-e\fR]
+[\fB\-E\ \fIcode:secret\fR]
+[\fB\-f\ \fIfamily\fR]
+[\fB\-h\fR]
+[\fB\-j\fR]
+[\fB\-l\ \fIlimit\fR]
+[\fB\-m\ \fImode\fR]
+[\fB\-n\ \fIruns_through_file\fR]
+[\fB\-p\ \fIport\fR]
+[\fB\-q\ \fInum_queries\fR]
+[\fB\-Q\ \fImax_qps\fR]
+[\fB\-s\ \fIserver_addr\fR]
+[\fB\-S\ \fIstats_interval\fR]
+[\fB\-t\ \fItimeout\fR]
+[\fB\-T\ \fIthreads\fR]
+[\fB\-u\fR]
+[\fB\-v\fR]
+[\fB\-W\fR]
+[\fB\-x\ \fIlocal_port\fR]
+[\fB\-y\ \fI[alg:]name:secret\fR]
+[\fB\-O\ \fIoption=value\fR]
+.ad
+.hy
+.SH DESCRIPTION
+\fBdnsperf\fR is a DNS server performance testing tool.
+It is primarily intended for measuring the performance of authoritative DNS
+servers, but it can also be used for measuring caching server performance in
+a closed laboratory environment.
+For testing caching servers resolving against the live Internet, the
+\fBresperf\fR program is preferred.
+
+It is recommended that \fBdnsperf\fR and the name server under test be run
+on separate machines, so that the CPU usage of \fBdnsperf\fR itself does not
+slow down the name server.
+The two machines should be connected with a fast network, preferably a
+dedicated Gigabit Ethernet segment.
+Testing through a router or firewall is not advisable.
+.SS "Configuring the name server"
+If using \fBdnsperf\fR to test an authoritative server, the name server
+under test should be set up to serve one or more zones similar in size and
+number to what the server is expected to serve in production.
+
+Also, be sure to turn off recursion in the server's configuration (in BIND
+8/9, specify "recursion no;" in the options block).
+In BIND 8, you should also specify "fetch-glue no;"; otherwise the server
+may attempt to retrieve glue information from the Internet during the test,
+slowing it down by an unpredictable factor.
+.SS "Constructing a query input file"
+A \fBdnsperf\fR input file should contain a large and realistic set of
+queries, on the order of ten thousand to a million.
+The input file contains one line per query, consisting of a domain name and
+an RR type name separated by a space.
+The class of the query is implicitly IN.
+
+When measuring the performance serving non-terminal zones such as the root
+zone or TLDs, note that such servers spend most of their time providing
+referral responses, not authoritative answers.
+Therefore, a realistic input file might consist mostly of queries for type
+A for names *below*, not at, the delegations present in the zone.
+For example, when testing the performance of a server configured to be
+authoritative for the top-level domain "fi.", which contains delegations
+for domains like "helsinki.fi" and "turku.fi", the input file could contain
+lines like
+.RS
+.hy 0
+
+.nf
+www.turku.fi A
+www.helsinki.fi A
+.fi
+.hy
+.RE
+
+where the "www" prefix ensures that the server will respond with a referral.
+Ideally, a realistic proportion of queries for nonexistent domains should be
+mixed in with those for existing ones, and the lines of the input file
+should be in a random order.
+
+To use space and dot, and any other character, in the domain name you can
+quote them using
+.I "\\\\DDD"
+where each D is a digit is the octet corresponding to the decimal number
+described by DDD (as per RFC1035 5.1. Format).
+Dots can be quoted without converting, for example:
+.RS
+.hy 0
+
+.nf
+space\\032space.com A
+tab\\009tab.com A
+dot\\.dot.com A
+.fi
+.hy
+.RE
+.SS "Constructing a dynamic update input file"
+To test dynamic update performance, \fBdnsperf\fR is run with the \fB\-u\fR
+option, and the input file is constructed of blocks of lines describing
+dynamic update messages.
+The first line in a block contains the zone name:
+.RS
+.hy 0
+
+.nf
+example.com
+.fi
+.hy
+.RE
+
+Subsequent lines contain prerequisites, if there are any.
+Prerequisites can specify that a name may or may not exist, an rrset may or
+may not exist, or an rrset exists and its rdata matches all specified rdata
+for that name and type.
+The keywords "require" and "prohibit" are followed by the appropriate
+information.
+All relative names are considered to be relative to the zone name.
+The following lines show the 5 types of prerequisites.
+.RS
+.hy 0
+
+.nf
+require a
+require a A
+require a A 1.2.3.4
+prohibit x
+prohibit x A
+.fi
+.hy
+.RE
+
+Subsequent lines contain records to be added, records to be deleted, rrsets
+to be deleted, or names to be deleted.
+The keywords "add" or "delete" are followed by the appropriate information.
+All relative names are considered to be relative to the zone name.
+The following lines show the 4 types of updates.
+.RS
+.hy 0
+
+.nf
+add x 3600 A 10.1.2.3
+delete y A 10.1.2.3
+delete z A
+delete w
+.fi
+.hy
+.RE
+
+Each update message is terminated by a line containing the command:
+.RS
+.hy 0
+
+.nf
+send
+.fi
+.hy
+.RE
+.SS "Running the tests"
+When running \fBdnsperf\fR, a data file (the \fB\-d\fR option) and server
+(the \fB\-s\fR option) will normally be specified.
+The output of dnsperf is mostly self-explanatory.
+Pay attention to the number of dropped packets reported - when running the
+test over a local Ethernet connection, it should be zero.
+If one or more packets has been dropped, there may be a problem with the
+network connection.
+In that case, the results should be considered suspect and the test repeated.
+.SS "Using DNS-over-HTTPS"
+When using DNS-over-HTTPS you must set the \fB-O doh\-uri=...\fR to something
+that works with the server you're sending to.
+Also note that the value for maximum outstanding queries will be used to
+control the maximum concurrent streams within the HTTP/2 connection.
+.SH OPTIONS
+
+\fB-a \fIlocal_addr\fR
+.br
+.RS
+Specifies the local address from which to send requests.
+The default is the wildcard address.
+.RE
+
+\fB-b \fIbufsize\fR
+.br
+.RS
+Sets the size of the socket's send and receive buffers, in kilobytes.
+If not specified, the operating system's default is used.
+.RE
+
+\fB-B\fR
+.br
+.RS
+Instructs \fBdnsperf\fR to read datafile in TCP-stream binary format
+as specified by RFC 1035 section "4.2.2. TCP usage".
+Each packet is preceded by 2-byte preambule which specifies length of
+the following DNS packet in network byte order, immediatelly followed
+by raw bytes of the packet.
+First two bytes of any packet should contain message ID and are
+overwritten by \fBdnsperf\fR on the fly. All other bytes are left
+intact.
+Packets shorter than two bytes are sent intact.
+Packets in datafile can contain arbitrary bytes and are not checked for
+validity.
+Malformed packets probably will not be responded to by servers and will
+cause timeouts.
+This option is mutually exclusive with \fB-D\fR, \fB-e\fR, \fB-E\fR,
+\fB-u\fR and \fB-y\fR.
+
+These binary datafiles can be generated using arbitrary TCP listeners such
+as \fBnetcat\fR (nc) and \fBsockat\fR.
+TCP must be used so that the length is prepended.
+Following example shows how to generate a datafile with two query and
+then using it, you need two terminals.
+
+.EX
+(terminal 1) $ nc -l 127.0.0.1 5300 > dns.blob
+(terminal 2) $ echo "example.com A" | dnsperf -s 127.0.0.1 -p 5300 -m tcp
+...wait for dnsperf to finish...
+(terminal 1) $ nc -l 127.0.0.1 5300 >> dns.blob
+(terminal 2) $ echo "example.com AAAA" | dnsperf -s 127.0.0.1 -p 5300 -m tcp
+...wait for dnsperf to finish...
+(terminal 1) $ dnsperf -B -d dns.blob -s $IP
+.EE
+.RE
+
+\fB-c \fIclients\fR
+.br
+.RS
+Act as multiple clients.
+Requests are sent from multiple sockets.
+The default is to act as 1 client.
+.RE
+
+\fB-d \fIdatafile\fR
+.br
+.RS
+Specifies the input data file.
+If not specified, \fBdnsperf\fR will read from standard input.
+.RE
+
+\fB-D\fR
+.br
+.RS
+Sets the DO (DNSSEC OK) bit [RFC3225] in all packets sent.
+This also enables EDNS0, which is required for DNSSEC.
+This option is mutually exclusive with \fB-B\fR.
+.RE
+
+\fB-e\fR
+.br
+.RS
+Enables EDNS0 [RFC2671], by adding an OPT record to all packets sent.
+This option is mutually exclusive with \fB-B\fR.
+.RE
+
+\fB-E \fIcode:value\fR
+.br
+.RS
+Add an EDNS [RFC2671] option to all packets sent, using the specified
+numeric option code and value expressed as a a hex-encoded string.
+This also enables EDNS0.
+This option is mutually exclusive with \fB-B\fR.
+.RE
+
+\fB-f \fIfamily\fR
+.br
+.RS
+Specifies the address family used for sending DNS packets.
+The possible values are "inet", "inet6", or "any".
+If "any" (the default value) is specified, \fBdnsperf\fR will use whichever
+address family is appropriate for the server it is sending packets to.
+.RE
+
+\fB-h\fR
+.br
+.RS
+Print a usage statement and exit.
+.RE
+
+\fB-j\fR
+.br
+.RS
+NOTE: Only available if built-in!
+
+Output streaming JSON objects for progress and statistics, see section
+JSON OUTPUT for full details.
+
+.EX
+$ src/dnsperf -d ../src/test/datafile -s 172.17.0.1 -j -S1 -Q1 -n 2
+{"start":{"version":"2.14.0","command_line":["dnsperf","-d","../src/test/datafile","-s","172.17.0.1","-j","-S1","-Q1","-n","2"],"queries":true,"server_addr":"172.17.0.1","server_port":53,"started":"Thu Dec 18 08:41:05 2025","maxruns":2}}
+{"rate":{"timestamp_usec":1766047266411099,"qps":0.99872063886161822}}
+{"rate":{"timestamp_usec":1766047267412462,"qps":1.997277710480615}}
+{"rate":{"timestamp_usec":1766047268413677,"qps":0.99878647443356328}}
+{"stop":{"reason":"end of file"}}
+{"statistics":{"sent":4,"completed":4,"lost":0,"request_avg_pktsize":28,"response_avg_pktsize":50,"run_time":4.0,"qps":0.99992450569981972,"latency":{"avg":0.02145,"min":0.00026400000000000002,"max":0.042937000000000003,"stddev":0.024424912669581168}}}
+.EE
+
+Can be used together with extended options \fBverbose-interval-stats\fR
+and \fBlatency-histogram\fR.
+
+This option is currently not compatible together with \fB-v\fR or \fB-W\fR.
+.RE
+
+\fB-l \fIlimit\fR
+.br
+.RS
+Specifies a time limit for the run, in seconds.
+This may cause the input to be read multiple times, or only some of the
+input to be read.
+The default behavior is to read the input once, and have no specific time
+limit.
+.RE
+
+\fB-m \fImode\fR
+.br
+.RS
+Specifies the transport mode to use, "udp", "tcp", "dot" or "doh".
+Default is "udp".
+.RE
+
+\fB-n \fIruns_through_file\fR
+.br
+.RS
+Run through the input file at most this many times.
+If no time limit is set, the file will be read exactly this number of
+times; if a time limit is set, the file may be read fewer times.
+.RE
+
+\fB-p \fIport\fR
+.br
+.RS
+Sets the port on which the DNS packets are sent.
+If not specified, the standard DNS port (udp/tcp 53, DoT 853, DoH 443) is used.
+.RE
+
+\fB-q \fInum_queries\fR
+.br
+.RS
+Sets the maximum number of outstanding requests.
+When this value is reached, \fBdnsperf\fR will not send any more requests
+until either responses are received or requests time out.
+The default value is 100.
+.RE
+
+\fB-Q \fImax_qps\fR
+.br
+.RS
+Limits the number of requests per second.
+There is no default limit.
+.RE
+
+\fB-s \fIserver_addr\fR
+.br
+.RS
+Specifies the name or address of the server to which requests will be sent.
+The default is the loopback address, 127.0.0.1.
+.RE
+
+\fB-S \fIstats_interval\fR
+.br
+.RS
+If this parameter is specified, a count of the number of answers received
+per second during the interval will be printed out every \fIstats_interval\fR
+seconds.
+.RE
+
+\fB-t \fItimeout\fR
+.br
+.RS
+Specifies the request timeout value, in seconds.
+\fBdnsperf\fR will no longer wait for a response to a particular request
+after this many seconds have elapsed.
+The default is 5 seconds.
+.RE
+
+\fB-T \fIthreads\fR
+.br
+.RS
+Run multiple client threads.
+By default, \fBdnsperf\fR uses one thread for sending requests and one
+thread for receiving responses.
+If this option is specified, \fBdnsperf\fR will instead use N pairs of
+send/receive threads.
+.RE
+
+\fB-u\fR
+.br
+.RS
+Instructs \fBdnsperf\fR to send DNS dynamic update messages, rather than
+queries.
+The format of the input file is different in this case; see the
+"Constructing a dynamic update input file" section for more details.
+This option is mutually exclusive with \fB-B\fR.
+.RE
+
+\fB-v\fR
+.br
+.RS
+Enables verbose mode.
+The DNS RCODE of each response will be reported to standard output when
+the response is received, as will the latency.
+If a query times out, it will be reported with the special string "T"
+instead of a normal DNS RCODE.
+If a query is interrupted, it will be reported with the special string "I".
+Additional information regarding network readiness and congestion will
+also be reported.
+.RE
+
+\fB-W\fR
+.br
+.RS
+Log warnings and errors to standard output instead of standard error making
+it easier for script, test and automation to capture all output.
+.RE
+
+\fB-x \fIlocal_port\fR
+.br
+.RS
+Specifies the local port from which to send requests.
+The default is the wildcard port (0).
+
+If acting as multiple clients and the wildcard port is used, each client
+will use a different random port.
+If a port is specified, the clients will use a range of ports starting with
+the specified one.
+.RE
+
+\fB-y \fI[alg:]name:secret\fR
+.br
+.RS
+Add a TSIG record [RFC2845] to all packets sent, using the specified TSIG
+key algorithm, name and secret, where the algorithm defaults to hmac-md5 and
+the secret is expressed as a base-64 encoded string.
+Available algorithms are: hmac-md5, hmac-sha1, hmac-sha224, hmac-sha256,
+hmac-sha384 and hmac-sha512.
+This option is mutually exclusive with \fB-B\fR.
+.RE
+
+\fB-O \fIoption=value\fR
+.br
+.RS
+Set an extended long option for various things to control different aspects
+of testing or protocol modules, see EXTENDED OPTIONS for list of available
+options.
+.RE
+.SH "EXTENDED OPTIONS"
+
+\fBdoh-uri=\fIURI\fR
+.br
+.RS
+The URI to use for DNS-over-HTTPS, default value is
+"https://localhost/dns-query".
+.RE
+
+\fBdoh-method=\fIHTTP_METHOD\fR
+.br
+.RS
+The HTTP method to use when querying with DNS-over-HTTPS, default is GET.
+Available methods are: GET, POST.
+.RE
+
+\fBtls-sni=\fISERVER_NAME\fR
+.br
+.RS
+The Server Name Indication (SNI) to use for TLS connections (such as DNS-over-TLS or DNS-over-HTTPS),
+defaults to leaving out the SNI extension in the client hello.
+.RE
+
+\fBsuppress=\fIMESSAGE[,MESSAGE,...]\fR
+.br
+.RS
+Suppress various messages and warnings that may be shown excessively in some
+situations, such as socket readiness when connecting to a slow service.
+Can suppress multiple types by listing them as a comma separated list.
+Following type are available.
+
+\fBtimeouts\fR: Suppress messages about queries being timed out
+.br
+\fBcongestion\fR: Suppress messages about network congestion
+.br
+\fBsendfailed\fR: Suppress messages about failure to send packets or if only parts of the packet were sent
+.br
+\fBsockready\fR: Suppress messages about socket readiness
+.br
+\fBunexpected\fR: Suppress messages about answers with an unexpected message ID
+.RE
+\fBnum-queries-per-conn=\fINUMBER\fR
+.br
+.RS
+This will limit the number of queries sent over a connection before
+triggering a re-connection. Once re-connected it will reset the counter and
+continue sending queries until the limit is reached again, triggering
+another re-connection and so on.
+Using this option will also enable counting number of responses received
+for each connection and once the limit is reached for sending queries it
+will wait until the same amount of responses has been received before
+re-connecting.
+Waiting for responses may timeout and the timeout used for this is the
+same as specified by \fB-t\fR.
+Note that this option is only useful for connection oriented protocols.
+.RE
+
+\fBverbose-interval-stats\fR
+.br
+.RS
+Change the statistics format of \fB-S\fR to that shown at end of run.
+
+\fIPlease note\fR: Min/max values for latency and connections are not
+available in interval statistics.
+Number of answers received within \fIstats_interval\fR can legitimately
+exceed number of queries sent, depending on answer latency, configured
+\fItimeout\fR, and \fIstats_interval\fR.
+
+Only available in \fBdnsperf\fR.
+.RE
+
+\fBlatency-histogram\fR
+.br
+.RS
+Print detailed latency histograms for DNS answers and connections.
+Latency is quantized into bins with roughly 3 % resolution, and latency
+range for individual bins increases logarithmically.
+This is done to to limit amount of memory required for histograms
+and also allows to visualize latency using logarithmic percentile histograms
+with minimal postprocessing.
+
+Only available in \fBdnsperf\fR and if compile time support was detected.
+.RE
+
+\fBqps-threshold-wait=<microseconds>\fR
+.br
+.RS
+When using \fB-Q\fR to rate limit queries sent, this option control the
+minimum time (in microseconds) there should be between queries to use
+\fInanosleep()\fR to wait until sending the next query.
+Setting to zero (0) will disable any call to \fInanosleep()\fR between
+sending queries.
+
+If the time between queries is lower then this, then no wait is performed
+in order to have more precision on when to send the next query.
+This is because during high QPS rate limiting it can take more time just
+calling the functions to wait for when to send the next query then the
+actually time between queries.
+
+If not set, the average call-time to \fInanosleep()\fR will be measured
+during startup.
+
+Only available in \fBdnsperf\fR.
+.RE
+.SH "JSON OUTPUT"
+Following data is available in the JSON output, dot is a delimiter for
+objects.
+
+\fBstart.version=\fIstring\fR
+.br
+.RS
+The version of \fBdnsperf\fR that generated the JSON.
+.RE
+
+\fBstart.command_line=\fI[string,...]\fR
+.br
+.RS
+The executed command and it's options as an array of strings.
+.RE
+
+\fBstart.updates=\fIboolean\fR
+.br
+.RS
+Set to true if what's was sent was dynamic updates.
+.RE
+
+\fBstart.queries=\fIboolean\fR
+.br
+.RS
+Set to true if what's was sent was DNS queries.
+.RE
+
+\fBstart.server_addr=\fIstring\fR
+.br
+.RS
+The server address sent to.
+.RE
+
+\fBstart.server_port=\fIint\fR
+.br
+.RS
+The server port sent to.
+.RE
+
+\fBstart.started=\fIstring\fR
+.br
+.RS
+Date and time when execution started.
+.RE
+
+\fBstart.timelimit=\fIdouble\fR
+.br
+.RS
+Set to a number of seconds if execution was limited to that.
+.RE
+
+\fBstart.maxruns=\fIint\fR
+.br
+.RS
+Set to a number of runs if execution was limited to that.
+.RE
+
+\fBrate.timestamp_usec=\fIuint\fR
+.br
+.RS
+Current UNIX timestamp in microseconds.
+.RE
+
+\fBrate.qps=\fIdouble\fR
+.br
+.RS
+Current queries per second sent.
+.RE
+
+\fBstop.reason=\fIstring\fR
+.br
+.RS
+The reason the execution was stopped.
+.RE
+
+\fBstatistics.interval=\fIboolean\fR
+.br
+.RS
+Set to true if this statistics object was outputted as requested by the
+extended option \fBverbose-interval-stats\fR.
+.RE
+
+\fBstatistics.sent=\fIuint\fR
+.br
+.RS
+The number of queries sent.
+.RE
+
+\fBstatistics.completed=\fIuint\fR
+.br
+.RS
+The number of responses received for queries sent.
+.RE
+
+\fBstatistics.lost=\fIuint\fR
+.br
+.RS
+The number of queries that didn't receive a response, either timed out or
+by other means.
+.RE
+
+\fBstatistics.interrupted=\fIuint\fR
+.br
+.RS
+The number of queries that was in some way interruped.
+.RE
+
+\fBstatistics.unexpected=\fIuint\fR
+.br
+.RS
+The number of responses received that did not have a corresponding query.
+.RE
+
+\fBstatistics.request_avg_pktsize=\fIuint\fR
+.br
+.RS
+Average packet size for queries.
+.RE
+
+\fBstatistics.response_avg_pktsize=\fIuint\fR
+.br
+.RS
+Average packet size for responses.
+.RE
+
+\fBstatistics.runtime=\fIdouble\fR
+.br
+.RS
+The run time in seconds.
+.RE
+
+\fBstatistics.qps=\fIdouble\fR
+.br
+.RS
+The queries per second.
+.RE
+
+\fBstatistics.latency=\fIlatency\fR
+.br
+.RS
+A query/response latency object, see below.
+.RE
+
+\fBstatistics.connection.attempts=\fIuint\fR
+.br
+.RS
+The number of connection attempts made.
+
+Only available when using connection oriented protocols.
+.RE
+
+\fBstatistics.connection.completed=\fIuint\fR
+.br
+.RS
+The number of connections established.
+
+Only available when using connection oriented protocols.
+.RE
+
+\fBstatistics.connection.latency=\fIlatency\fR
+.br
+.RS
+A connection latency object, see below.
+
+Only available when using connection oriented protocols.
+.RE
+
+\fBdns_over_https.status_codes=\fI{ http_status_code: count, ... }\fR
+.br
+.RS
+Specific statistics when DNS-over-HTTPS protocol is used.
+
+An object with a counter for each of the HTTP Status codes received.
+.RE
+
+\fBlatency.avg=\fIdouble\fR
+.br
+.RS
+Average latency.
+.RE
+
+\fBlatency.min=\fIdouble\fR
+.br
+.RS
+Minimum latency.
+.RE
+
+\fBlatency.max=\fIdouble\fR
+.br
+.RS
+Maximum latency
+.RE
+
+\fBlatency.stddev=\fIdouble\fR
+.br
+.RS
+Standard deviation of the latency.
+.RE
+
+\fBlatency.histogram=\fIhistogram\fR
+.br
+.RS
+A histogram of the latency, see below.
+.RE
+
+\fBhistogram=\fI[[min, max, count],...]\fR
+.br
+.RS
+A histogram object, see extended option \fBlatency-histogram\fR for more
+information.
+.RE
+
+.SH "SEE ALSO"
+\fBresperf\fR(1)
+.SH AUTHOR
+Nominum, Inc.
+.LP
+Maintained by DNS-OARC
+.LP
+.RS
+.I https://www.dns-oarc.net/
+.RE
+.LP
+.SH BUGS
+For issues and feature requests please use:
+.LP
+.RS
+\fI@PACKAGE_URL@\fP
+.RE
+.LP
+For question and help please use:
+.LP
+.RS
+\fI@PACKAGE_BUGREPORT@\fP
+.RE
+.LP
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/***
+ *** DNS Performance Testing Tool
+ ***/
+
+#include "config.h"
+
+#include "net.h"
+#include "datafile.h"
+#include "dns.h"
+#include "log.h"
+#include "opt.h"
+#include "os.h"
+#include "util.h"
+#include "list.h"
+#include "buffer.h"
+#if HAVE_STDATOMIC_H
+#include "ext/hg64.h"
+#define USE_HISTOGRAMS
+#endif
+
+#include <inttypes.h>
+#include <errno.h>
+#include <math.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <openssl/ssl.h>
+#include <openssl/conf.h>
+#include <openssl/err.h>
+#ifdef HAVE_JSON_C
+#include <json-c/json.h>
+#endif
+
+#define HISTOGRAM_SIGBITS 5 /* about 3 % latency precision */
+
+#define DEFAULT_SERVER_NAME "127.0.0.1"
+#define DEFAULT_SERVER_PORT 53
+#define DEFAULT_SERVER_DOT_PORT 853
+#define DEFAULT_SERVER_DOH_PORT 443
+#define DEFAULT_SERVER_PORTS "udp/tcp 53, DoT 853 or DoH 443"
+#define DEFAULT_LOCAL_PORT 0
+#define DEFAULT_MAX_OUTSTANDING 100
+#define DEFAULT_TIMEOUT 5
+
+#define TIMEOUT_CHECK_TIME 100000
+
+#define MAX_INPUT_DATA (64 * 1024) + 2
+
+#define MAX_SOCKETS 256
+
+#define RECV_BATCH_SIZE 16
+
+typedef struct {
+ int argc;
+ char** argv;
+ int family;
+ uint32_t clients;
+ uint32_t threads;
+ uint32_t maxruns;
+ uint64_t timelimit;
+ perf_sockaddr_t server_addr;
+ perf_sockaddr_t local_addr;
+ uint64_t timeout;
+ uint32_t bufsize;
+ bool edns;
+ bool dnssec;
+ const char* tsigkey;
+ perf_ednsoption_t* edns_option;
+ uint32_t max_outstanding;
+ uint32_t max_qps;
+ uint64_t stats_interval;
+ bool verbose_interval_stats;
+ bool updates;
+ bool binary_input;
+ perf_input_format_t input_format;
+ bool verbose;
+ enum perf_net_mode mode;
+ perf_suppress_t suppress;
+ size_t num_queries_per_conn;
+#ifdef USE_HISTOGRAMS
+ bool latency_histogram;
+#endif
+ int qps_threshold_wait;
+#ifdef HAVE_JSON_C
+ bool json;
+#endif
+} config_t;
+
+typedef struct {
+ uint64_t start_time;
+ uint64_t end_time;
+ uint64_t stop_time;
+ struct timespec stop_time_ns;
+} times_t;
+
+#define DNSPERF_STATS_RCODECOUNTS 16
+typedef struct {
+ uint64_t rcodecounts[DNSPERF_STATS_RCODECOUNTS];
+
+ uint64_t num_sent;
+ uint64_t num_interrupted;
+ uint64_t num_timedout;
+ uint64_t num_completed;
+ uint64_t num_unexpected;
+
+ uint64_t total_request_size;
+ uint64_t total_response_size;
+
+ uint64_t latency_sum;
+ uint64_t latency_sum_squares;
+ uint64_t latency_min;
+ uint64_t latency_max;
+
+ uint64_t num_conn_attempts;
+ uint64_t num_conn_completed;
+
+ uint64_t conn_latency_sum;
+ uint64_t conn_latency_sum_squares;
+ uint64_t conn_latency_min;
+ uint64_t conn_latency_max;
+
+#ifdef USE_HISTOGRAMS
+ hg64* latency;
+ hg64* conn_latency;
+#endif
+} stats_t;
+
+typedef perf_list(struct query_info) query_list;
+
+typedef struct query_info {
+ uint64_t timestamp;
+ query_list* list;
+ char* desc;
+ struct perf_net_socket* sock;
+ /*
+ * This link links the query into the list of outstanding
+ * queries or the list of available query IDs.
+ */
+ perf_link(struct query_info);
+} query_info;
+
+#define NQIDS 65536
+
+typedef struct {
+ query_info queries[NQIDS];
+ query_list outstanding_queries;
+ query_list unused_queries;
+
+ pthread_t sender;
+ pthread_t receiver;
+
+ pthread_mutex_t lock;
+ pthread_cond_t cond;
+
+ unsigned int nsocks;
+ int current_sock;
+ struct perf_net_socket** socks;
+
+ bool done_sending;
+ uint64_t done_send_time;
+
+ const config_t* config;
+ const times_t* times;
+ stats_t stats;
+
+ uint32_t max_outstanding;
+ uint32_t max_qps;
+
+ uint64_t last_recv;
+} threadinfo_t;
+
+static threadinfo_t* threads;
+
+static pthread_mutex_t start_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t start_cond = PTHREAD_COND_INITIALIZER;
+static bool started;
+
+static bool interrupted = false;
+
+static int threadpipe[2];
+static int mainpipe[2];
+static int intrpipe[2];
+
+static perf_datafile_t* input;
+
+const char* progname = "dnsperf";
+
+static void
+handle_sigint(int sig)
+{
+ (void)sig;
+ if (write(intrpipe[1], "", 1)) { // lgtm [cpp/empty-block]
+ }
+}
+
+static void
+print_initial_status(const config_t* config)
+{
+ time_t now;
+ char buf[255], ct[32];
+ int i;
+
+ perf_sockaddr_format(&config->server_addr, buf, sizeof(buf));
+ now = time(NULL);
+
+#ifdef HAVE_JSON_C
+ if (config->json) {
+ int ret = 0;
+ struct json_object* start = json_object_new_object();
+
+ ret |= json_object_object_add(start, "version", json_object_new_string(PACKAGE_VERSION));
+
+ struct json_object* command_line = json_object_new_array();
+ ret |= json_object_array_add(command_line, json_object_new_string(progname));
+ for (i = 1; i < config->argc; i++)
+ ret |= json_object_array_add(command_line, json_object_new_string(config->argv[i]));
+ ret |= json_object_object_add(start, "command_line", command_line);
+
+ ret |= json_object_object_add(start, config->updates ? "updates" : "queries", json_object_new_boolean(1));
+ ret |= json_object_object_add(start, "server_addr", json_object_new_string(buf));
+ ret |= json_object_object_add(start, "server_port", json_object_new_int(perf_sockaddr_port(&config->server_addr)));
+
+ ctime_r(&now, ct);
+ char* nl = strchr(ct, '\n');
+ if (nl)
+ *nl = 0; // Remove added new-line by ctime_r()
+ ret |= json_object_object_add(start, "started", json_object_new_string(ct));
+
+ if (config->timelimit)
+ ret |= json_object_object_add(start, "timelimit", json_object_new_double(config->timelimit / MILLION));
+ if (config->maxruns)
+ ret |= json_object_object_add(start, "maxruns", json_object_new_int(config->maxruns));
+
+ struct json_object* json = json_object_new_object();
+ ret |= json_object_object_add(json, "start", start);
+ if (!ret) {
+ printf("%s\n", json_object_to_json_string_ext(json, JSON_C_TO_STRING_NOSLASHESCAPE));
+ } else {
+ perf_log_fatal("unable to construct JSON output");
+ }
+ json_object_put(json);
+ return;
+ }
+#endif
+
+ printf("[Status] Command line: %s", progname);
+ for (i = 1; i < config->argc; i++)
+ printf(" %s", config->argv[i]);
+ printf("\n");
+
+ if (perf_sockaddr_isinet6(&config->server_addr)) {
+ printf("[Status] Sending %s (to [%s]:%d)\n",
+ config->updates ? "updates" : "queries", buf, perf_sockaddr_port(&config->server_addr));
+ } else {
+ printf("[Status] Sending %s (to %s:%d)\n",
+ config->updates ? "updates" : "queries", buf, perf_sockaddr_port(&config->server_addr));
+ }
+
+ printf("[Status] Started at: %s", ctime_r(&now, ct));
+
+ printf("[Status] Stopping after ");
+ if (config->timelimit)
+ printf("%u.%06u seconds",
+ (unsigned int)(config->timelimit / MILLION),
+ (unsigned int)(config->timelimit % MILLION));
+ if (config->timelimit && config->maxruns)
+ printf(" or ");
+ if (config->maxruns)
+ printf("%u run%s through file", config->maxruns,
+ config->maxruns == 1 ? "" : "s");
+ printf("\n");
+}
+
+static void
+print_final_status(const config_t* config)
+{
+ const char* reason;
+
+ if (interrupted)
+ reason = "interruption";
+ else if (config->maxruns > 0 && perf_datafile_nruns(input) == config->maxruns)
+ reason = "end of file";
+ else
+ reason = "time limit";
+
+#ifdef HAVE_JSON_C
+ if (config->json) {
+ int ret = 0;
+
+ struct json_object* stop = json_object_new_object();
+
+ ret |= json_object_object_add(stop, "reason", json_object_new_string(reason));
+
+ struct json_object* json = json_object_new_object();
+ ret |= json_object_object_add(json, "stop", stop);
+ if (!ret) {
+ printf("%s\n", json_object_to_json_string_ext(json, JSON_C_TO_STRING_NOSLASHESCAPE));
+ } else {
+ perf_log_fatal("unable to construct JSON output");
+ }
+ json_object_put(json);
+ return;
+ }
+#endif
+
+ printf("[Status] Testing complete (%s)\n", reason);
+ printf("\n");
+}
+
+static double
+stddev(uint64_t sum_of_squares, uint64_t sum, uint64_t total)
+{
+ double squared;
+
+ squared = (double)sum * (double)sum;
+ return sqrt((sum_of_squares - (squared / total)) / (total - 1));
+}
+
+static void
+diff_stats(const config_t* config, stats_t* last, stats_t* now, stats_t* diff)
+{
+ int i = 0;
+ for (; i < DNSPERF_STATS_RCODECOUNTS; i++) {
+ diff->rcodecounts[i] = now->rcodecounts[i] - last->rcodecounts[i];
+ }
+
+ diff->num_sent = now->num_sent - last->num_sent;
+ diff->num_interrupted = now->num_interrupted - last->num_interrupted;
+ diff->num_timedout = now->num_timedout - last->num_timedout;
+ diff->num_completed = now->num_completed - last->num_completed;
+ diff->num_unexpected = now->num_unexpected - last->num_unexpected;
+
+ diff->total_request_size = now->total_request_size - last->total_request_size;
+ diff->total_response_size = now->total_response_size - last->total_response_size;
+
+ diff->latency_sum = now->latency_sum - last->latency_sum;
+ diff->latency_sum_squares = now->latency_sum_squares - last->latency_sum_squares;
+ diff->latency_min = 0; /* not enough data */
+ diff->latency_max = 0;
+
+ diff->num_conn_attempts = now->num_conn_attempts - last->num_conn_attempts;
+ diff->num_conn_completed = now->num_conn_completed - last->num_conn_completed;
+
+ diff->conn_latency_sum = now->conn_latency_sum - last->conn_latency_sum;
+ diff->conn_latency_sum_squares = now->conn_latency_sum_squares - last->conn_latency_sum_squares;
+ diff->conn_latency_min = 0;
+ diff->conn_latency_max = 0;
+
+#ifdef USE_HISTOGRAMS
+ if (config->latency_histogram) {
+ free(diff->latency);
+ diff->latency = hg64_create(HISTOGRAM_SIGBITS);
+ if (last->latency) {
+ hg64_diff(now->latency, last->latency, diff->latency);
+ } else { /* first sample */
+ hg64_merge(diff->latency, now->latency);
+ }
+ hg64_get(diff->latency, hg64_min_key(diff->latency), &diff->latency_min, NULL, NULL);
+ hg64_get(diff->latency, hg64_max_key(diff->latency), NULL, &diff->latency_max, NULL);
+
+ free(diff->conn_latency);
+ diff->conn_latency = hg64_create(HISTOGRAM_SIGBITS);
+ if (last->conn_latency) {
+ hg64_diff(now->conn_latency, last->conn_latency, diff->conn_latency);
+ } else { /* first sample */
+ hg64_merge(diff->conn_latency, now->conn_latency);
+ }
+ hg64_get(diff->conn_latency, hg64_min_key(diff->conn_latency), &diff->conn_latency_min, NULL, NULL);
+ hg64_get(diff->conn_latency, hg64_max_key(diff->conn_latency), NULL, &diff->conn_latency_max, NULL);
+ }
+#endif
+}
+
+#ifdef USE_HISTOGRAMS
+static void
+print_histogram(hg64* histogram, const char* const desc)
+{
+ printf(" Latency bucket (s): %s\n", desc);
+ uint64_t pmin, pmax, pcount;
+ for (unsigned key = 0;
+ hg64_get(histogram, key, &pmin, &pmax, &pcount) == true;
+ key = hg64_next(histogram, key)) {
+ if (pcount == 0)
+ continue;
+ printf(" %" PRIu64 ".%06" PRIu64 " - %" PRIu64 ".%06" PRIu64 ": %" PRIu64 "\n",
+ pmin / MILLION,
+ pmin % MILLION,
+ pmax / MILLION,
+ pmax % MILLION,
+ pcount);
+ };
+}
+
+#ifdef HAVE_JSON_C
+static struct json_object*
+histogram2json(hg64* histogram)
+{
+ struct json_object* hg_array = json_object_new_array();
+
+ uint64_t pmin, pmax, pcount;
+ for (unsigned key = 0;
+ hg64_get(histogram, key, &pmin, &pmax, &pcount) == true;
+ key = hg64_next(histogram, key)) {
+ if (pcount == 0)
+ continue;
+
+ struct json_object* hg_ent = json_object_new_array();
+ json_object_array_add(hg_ent, json_object_new_double((double)pmin / MILLION));
+ json_object_array_add(hg_ent, json_object_new_double((double)pmax / MILLION));
+ json_object_array_add(hg_ent, json_object_new_uint64(pcount));
+ json_object_array_add(hg_array, hg_ent);
+ }
+
+ return hg_array;
+}
+#endif
+#endif
+
+/*
+ * now != 0 is call to print stats in the middle of test run.
+ * min-max values are not available on per-interval basis, so skip them.
+ */
+static void
+print_statistics(const config_t* config, const times_t* times, stats_t* stats, uint64_t now, uint64_t interval_time)
+{
+ const char* units;
+ uint64_t run_time;
+ bool first_rcode;
+ uint64_t latency_avg;
+ unsigned int i;
+
+ units = config->updates ? "Updates" : "Queries";
+
+ if (now)
+ run_time = now - times->start_time;
+ else
+ run_time = times->end_time - times->start_time;
+
+#ifdef HAVE_JSON_C
+ if (config->json) {
+ int ret = 0;
+
+ struct json_object* statistics = json_object_new_object();
+
+ if (now)
+ ret |= json_object_object_add(statistics, "interval", json_object_new_boolean(1));
+
+ ret |= json_object_object_add(statistics, "sent", json_object_new_uint64(stats->num_sent));
+ ret |= json_object_object_add(statistics, "completed", json_object_new_uint64(stats->num_completed));
+ ret |= json_object_object_add(statistics, "lost", json_object_new_uint64(stats->num_timedout));
+ if (stats->num_interrupted > 0)
+ ret |= json_object_object_add(statistics, "interrupted", json_object_new_uint64(stats->num_interrupted));
+ if (stats->num_unexpected > 0)
+ ret |= json_object_object_add(statistics, "unexpected", json_object_new_uint64(stats->num_unexpected));
+
+ struct json_object* rcodes = json_object_new_object();
+ for (i = 0; i < DNSPERF_STATS_RCODECOUNTS; i++) {
+ if (stats->rcodecounts[i] == 0)
+ continue;
+ ret |= json_object_object_add(rcodes, perf_dns_rcode_strings[i], json_object_new_uint64(stats->rcodecounts[i]));
+ }
+
+ ret |= json_object_object_add(statistics, "request_avg_pktsize", json_object_new_uint64((unsigned int)PERF_SAFE_DIV(stats->total_request_size, stats->num_sent)));
+ ret |= json_object_object_add(statistics, "response_avg_pktsize", json_object_new_uint64((unsigned int)PERF_SAFE_DIV(stats->total_response_size, stats->num_completed)));
+
+ ret |= json_object_object_add(statistics, "run_time", json_object_new_double(run_time / MILLION));
+ ret |= json_object_object_add(statistics, "qps", json_object_new_double(PERF_SAFE_DIV(stats->num_completed, (((double)(now ? interval_time : run_time) / MILLION)))));
+
+ {
+ struct json_object* latency = json_object_new_object();
+ ret |= json_object_object_add(latency, "avg", json_object_new_double((double)PERF_SAFE_DIV(stats->latency_sum, stats->num_completed) / MILLION));
+ if (!now) {
+ ret |= json_object_object_add(latency, "min", json_object_new_double((double)stats->latency_min / MILLION));
+ ret |= json_object_object_add(latency, "max", json_object_new_double((double)stats->latency_max / MILLION));
+ }
+
+ if (stats->num_completed > 1) {
+ ret |= json_object_object_add(latency, "stddev", json_object_new_double(stddev(stats->latency_sum_squares, stats->latency_sum, stats->num_completed) / MILLION));
+#ifdef USE_HISTOGRAMS
+ if (config->latency_histogram)
+ ret |= json_object_object_add(latency, "histogram", histogram2json(stats->latency));
+#endif
+ }
+
+ ret |= json_object_object_add(statistics, "latency", latency);
+ }
+
+ if (stats->num_conn_completed || stats->num_conn_attempts) {
+ struct json_object* connection = json_object_new_object();
+ ret |= json_object_object_add(connection, "attempts", json_object_new_uint64(stats->num_conn_attempts));
+ ret |= json_object_object_add(connection, "completed", json_object_new_uint64(stats->num_conn_completed));
+
+ struct json_object* latency = json_object_new_object();
+ ret |= json_object_object_add(latency, "avg", json_object_new_double((double)PERF_SAFE_DIV(stats->conn_latency_sum, stats->num_conn_completed) / MILLION));
+ if (!now) {
+ ret |= json_object_object_add(latency, "min", json_object_new_double((double)stats->conn_latency_min / MILLION));
+ ret |= json_object_object_add(latency, "max", json_object_new_double((double)stats->conn_latency_max / MILLION));
+ }
+
+ if (stats->num_conn_completed > 1) {
+ ret |= json_object_object_add(latency, "stddev", json_object_new_double(stddev(stats->conn_latency_sum_squares, stats->conn_latency_sum, stats->num_conn_completed) / MILLION));
+#ifdef USE_HISTOGRAMS
+ if (config->latency_histogram)
+ ret |= json_object_object_add(latency, "histogram", histogram2json(stats->conn_latency));
+#endif
+ }
+ ret |= json_object_object_add(connection, "latency", latency);
+
+ ret |= json_object_object_add(statistics, "connection", connection);
+ }
+
+ struct json_object* json = json_object_new_object();
+ ret |= json_object_object_add(json, "statistics", statistics);
+ if (!ret) {
+ printf("%s\n", json_object_to_json_string_ext(json, JSON_C_TO_STRING_NOSLASHESCAPE));
+ } else {
+ perf_log_fatal("unable to construct JSON output");
+ }
+ json_object_put(json);
+ fflush(stdout);
+ return;
+ }
+#endif
+
+ printf("%sStatistics:\n\n", now ? "Interval " : "");
+
+ printf(" %s sent: %" PRIu64 "\n",
+ units, stats->num_sent);
+ printf(" %s completed: %" PRIu64 " (%.2lf%%)\n",
+ units, stats->num_completed,
+ PERF_SAFE_DIV(100.0 * stats->num_completed, stats->num_sent));
+ printf(" %s lost: %" PRIu64 " (%.2lf%%)\n",
+ units, stats->num_timedout,
+ PERF_SAFE_DIV(100.0 * stats->num_timedout, stats->num_sent));
+ if (stats->num_interrupted > 0)
+ printf(" %s interrupted: %" PRIu64 " (%.2lf%%)\n",
+ units, stats->num_interrupted,
+ PERF_SAFE_DIV(100.0 * stats->num_interrupted, stats->num_sent));
+ if (stats->num_unexpected > 0)
+ printf(" Unexpected IDs: %" PRIu64 " (%.2lf%%)\n",
+ stats->num_unexpected,
+ PERF_SAFE_DIV(100.0 * stats->num_unexpected, stats->num_sent));
+
+ printf("\n");
+
+ printf(" Response codes: ");
+ first_rcode = true;
+ for (i = 0; i < DNSPERF_STATS_RCODECOUNTS; i++) {
+ if (stats->rcodecounts[i] == 0)
+ continue;
+ if (first_rcode)
+ first_rcode = false;
+ else
+ printf(", ");
+ printf("%s %" PRIu64 " (%.2lf%%)",
+ perf_dns_rcode_strings[i], stats->rcodecounts[i],
+ (stats->rcodecounts[i] * 100.0) / stats->num_completed);
+ }
+ printf("\n");
+
+ printf(" Average packet size: request %u, response %u\n",
+ (unsigned int)PERF_SAFE_DIV(stats->total_request_size, stats->num_sent),
+ (unsigned int)PERF_SAFE_DIV(stats->total_response_size,
+ stats->num_completed));
+ printf(" Run time (s): %u.%06u\n",
+ (unsigned int)(run_time / MILLION),
+ (unsigned int)(run_time % MILLION));
+ printf(" %s per second: %.6lf\n", units,
+ PERF_SAFE_DIV(stats->num_completed, (((double)(now ? interval_time : run_time) / MILLION))));
+
+ printf("\n");
+
+ latency_avg = PERF_SAFE_DIV(stats->latency_sum, stats->num_completed);
+ printf(" Average Latency (s): %u.%06u",
+ (unsigned int)(latency_avg / MILLION),
+ (unsigned int)(latency_avg % MILLION));
+ if (!now) {
+ printf(" (min %u.%06u, max %u.%06u)\n",
+ (unsigned int)(stats->latency_min / MILLION),
+ (unsigned int)(stats->latency_min % MILLION),
+ (unsigned int)(stats->latency_max / MILLION),
+ (unsigned int)(stats->latency_max % MILLION));
+ } else {
+ printf("\n");
+ }
+
+ if (stats->num_completed > 1) {
+ printf(" Latency StdDev (s): %f\n",
+ stddev(stats->latency_sum_squares, stats->latency_sum,
+ stats->num_completed)
+ / MILLION);
+#ifdef USE_HISTOGRAMS
+ if (config->latency_histogram)
+ print_histogram(stats->latency, "answer count");
+#endif
+ }
+
+ printf("\n");
+
+ if (!stats->num_conn_completed && !stats->num_conn_attempts) {
+ fflush(stdout);
+ return;
+ }
+
+ printf("Connection Statistics:\n\n");
+ printf(" Connection attempts: %" PRIu64 " (%" PRIu64 " successful, %.2lf%%)\n\n",
+ stats->num_conn_attempts,
+ stats->num_conn_completed,
+ PERF_SAFE_DIV(100.0 * stats->num_conn_completed, stats->num_conn_attempts));
+ latency_avg = PERF_SAFE_DIV(stats->conn_latency_sum, stats->num_conn_completed);
+ printf(" Average Latency (s): %u.%06u",
+ (unsigned int)(latency_avg / MILLION),
+ (unsigned int)(latency_avg % MILLION));
+ if (!now) {
+ printf(" (min %u.%06u, max %u.%06u)\n",
+ (unsigned int)(stats->conn_latency_min / MILLION),
+ (unsigned int)(stats->conn_latency_min % MILLION),
+ (unsigned int)(stats->conn_latency_max / MILLION),
+ (unsigned int)(stats->conn_latency_max % MILLION));
+ } else {
+ printf("\n");
+ }
+ if (stats->num_conn_completed > 1) {
+ printf(" Latency StdDev (s): %f\n",
+ stddev(stats->conn_latency_sum_squares, stats->conn_latency_sum, stats->num_conn_completed) / MILLION);
+#ifdef USE_HISTOGRAMS
+ if (config->latency_histogram)
+ print_histogram(stats->conn_latency, "connection count");
+#endif
+ }
+
+ printf("\n");
+ fflush(stdout);
+}
+
+/*
+ * Caller must free() stats->latency and stats->conn_latency.
+ */
+static void
+sum_stats(const config_t* config, stats_t* total)
+{
+ unsigned int i, j;
+
+ memset(total, 0, sizeof(*total));
+#ifdef USE_HISTOGRAMS
+ if (config->latency_histogram) {
+ total->latency = hg64_create(HISTOGRAM_SIGBITS);
+ total->conn_latency = hg64_create(HISTOGRAM_SIGBITS);
+ }
+#endif
+
+ for (i = 0; i < config->threads; i++) {
+ stats_t* stats = &threads[i].stats;
+#ifdef USE_HISTOGRAMS
+ if (config->latency_histogram) {
+ hg64_merge(total->latency, stats->latency);
+ hg64_merge(total->conn_latency, stats->conn_latency);
+ }
+#endif
+
+ for (j = 0; j < DNSPERF_STATS_RCODECOUNTS; j++)
+ total->rcodecounts[j] += stats->rcodecounts[j];
+
+ total->num_sent += stats->num_sent;
+ total->num_interrupted += stats->num_interrupted;
+ total->num_timedout += stats->num_timedout;
+ total->num_completed += stats->num_completed;
+ total->num_unexpected += stats->num_unexpected;
+
+ total->total_request_size += stats->total_request_size;
+ total->total_response_size += stats->total_response_size;
+
+ total->latency_sum += stats->latency_sum;
+ total->latency_sum_squares += stats->latency_sum_squares;
+ if (stats->latency_min < total->latency_min || i == 0)
+ total->latency_min = stats->latency_min;
+ if (stats->latency_max > total->latency_max)
+ total->latency_max = stats->latency_max;
+
+ total->num_conn_completed += stats->num_conn_completed;
+ total->num_conn_attempts += stats->num_conn_attempts;
+
+ total->conn_latency_sum += stats->conn_latency_sum;
+ total->conn_latency_sum_squares += stats->conn_latency_sum_squares;
+ if (stats->conn_latency_min < total->conn_latency_min || i == 0)
+ total->conn_latency_min = stats->conn_latency_min;
+ if (stats->conn_latency_max > total->conn_latency_max)
+ total->conn_latency_max = stats->conn_latency_max;
+ }
+}
+
+static char*
+stringify(unsigned int value)
+{
+ static char buf[20];
+
+ snprintf(buf, sizeof(buf), "%u", value);
+ return buf;
+}
+
+static int
+measure_nanosleep(config_t* config)
+{
+ struct timespec start, stop, wait = { 0, 0 };
+ int err;
+
+ int i = 100;
+ if ((err = clock_gettime(CLOCK_REALTIME, &start))) {
+ return err;
+ }
+ for (; i; i--) {
+ if ((err = nanosleep(&wait, NULL))) {
+ return err;
+ }
+ }
+ if ((err = clock_gettime(CLOCK_REALTIME, &stop))) {
+ return err;
+ }
+
+ // Total time for 100 nanosleep() + 2 clock_gettime()
+ config->qps_threshold_wait = ((stop.tv_sec - start.tv_sec) * 1000000000 + stop.tv_nsec - start.tv_nsec)
+ // divided by 100 runs
+ / 100
+ // add fudge
+ * 3
+ // converted to microseconds
+ / 1000;
+ if (config->qps_threshold_wait < 0) {
+ config->qps_threshold_wait = 0;
+ }
+
+ return 0;
+}
+
+static void
+setup(int argc, char** argv, config_t* config)
+{
+ const char* family = NULL;
+ const char* server_name = DEFAULT_SERVER_NAME;
+ in_port_t server_port = 0;
+ const char* local_name = NULL;
+ in_port_t local_port = DEFAULT_LOCAL_PORT;
+ const char* filename = NULL;
+ const char* edns_option = NULL;
+ const char* mode = 0;
+ const char* doh_uri = DEFAULT_DOH_URI;
+ const char* doh_method = DEFAULT_DOH_METHOD;
+ const char* local_suppress = 0;
+ const char* tls_sni = 0;
+
+ memset(config, 0, sizeof(*config));
+ config->argc = argc;
+ config->argv = argv;
+
+ config->family = AF_UNSPEC;
+ config->clients = 1;
+ config->threads = 1;
+ config->timeout = DEFAULT_TIMEOUT * MILLION;
+ config->max_outstanding = DEFAULT_MAX_OUTSTANDING;
+ config->mode = sock_udp;
+
+ config->qps_threshold_wait = -1;
+
+ perf_opt_add('f', perf_opt_string, "family",
+ "address family of DNS transport, inet or inet6", "any",
+ &family);
+ perf_opt_add('m', perf_opt_string, "mode", "set transport mode: udp, tcp, dot or doh", "udp", &mode);
+ perf_opt_add('s', perf_opt_string, "server_addr",
+ "the server to query", DEFAULT_SERVER_NAME, &server_name);
+ perf_opt_add('p', perf_opt_port, "port",
+ "the port on which to query the server",
+ DEFAULT_SERVER_PORTS, &server_port);
+ perf_opt_add('a', perf_opt_string, "local_addr",
+ "the local address from which to send queries", NULL,
+ &local_name);
+ perf_opt_add('x', perf_opt_port, "local_port",
+ "the local port from which to send queries",
+ stringify(DEFAULT_LOCAL_PORT), &local_port);
+ perf_opt_add('d', perf_opt_string, "datafile",
+ "the input data file", "stdin", &filename);
+ perf_opt_add('c', perf_opt_uint, "clients",
+ "the number of clients to act as", NULL,
+ &config->clients);
+ perf_opt_add('T', perf_opt_uint, "threads",
+ "the number of threads to run", NULL,
+ &config->threads);
+ perf_opt_add('n', perf_opt_uint, "maxruns",
+ "run through input at most N times", NULL,
+ &config->maxruns);
+ perf_opt_add('l', perf_opt_timeval, "timelimit",
+ "run for at most this many seconds", NULL,
+ &config->timelimit);
+ perf_opt_add('b', perf_opt_uint, "buffer_size",
+ "socket send/receive buffer size in kilobytes", NULL,
+ &config->bufsize);
+ perf_opt_add('t', perf_opt_timeval, "timeout",
+ "the timeout for query completion in seconds",
+ stringify(DEFAULT_TIMEOUT), &config->timeout);
+ perf_opt_add('e', perf_opt_boolean, NULL,
+ "enable EDNS 0", NULL, &config->edns);
+ perf_opt_add('E', perf_opt_string, "code:value",
+ "send EDNS option", NULL, &edns_option);
+ perf_opt_add('D', perf_opt_boolean, NULL,
+ "set the DNSSEC OK bit (implies EDNS)", NULL,
+ &config->dnssec);
+ perf_opt_add('y', perf_opt_string, "[alg:]name:secret",
+ "the TSIG algorithm, name and secret (base64)", NULL,
+ &config->tsigkey);
+ perf_opt_add('q', perf_opt_uint, "num_queries",
+ "the maximum number of queries outstanding",
+ stringify(DEFAULT_MAX_OUTSTANDING),
+ &config->max_outstanding);
+ perf_opt_add('Q', perf_opt_uint, "max_qps",
+ "limit the number of queries per second", NULL,
+ &config->max_qps);
+ perf_opt_add('S', perf_opt_timeval, "stats_interval",
+ "print qps statistics every N seconds",
+ NULL, &config->stats_interval);
+ perf_opt_add('u', perf_opt_boolean, NULL,
+ "send dynamic updates instead of queries",
+ NULL, &config->updates);
+ perf_opt_add('B', perf_opt_boolean, NULL,
+ "read input file as TCP-stream binary format",
+ NULL, &config->binary_input);
+ perf_opt_add('v', perf_opt_boolean, NULL,
+ "verbose: report each query and additional information to stdout",
+ NULL, &config->verbose);
+#ifdef HAVE_JSON_C
+ perf_opt_add('j', perf_opt_boolean, NULL,
+ "use JSON output for run information and statistics",
+ NULL, &config->json);
+#endif
+ perf_long_opt_add("doh-uri", perf_opt_string, "doh_uri",
+ "the URI to use for DNS-over-HTTPS", DEFAULT_DOH_URI, &doh_uri);
+ perf_long_opt_add("doh-method", perf_opt_string, "doh_method",
+ "the HTTP method to use for DNS-over-HTTPS: GET or POST", DEFAULT_DOH_METHOD, &doh_method);
+ perf_long_opt_add("suppress", perf_opt_string, "message[,message,...]",
+ "suppress messages/warnings, see man-page for list of message types", NULL, &local_suppress);
+ perf_long_opt_add("num-queries-per-conn", perf_opt_uint, "queries",
+ "Number of queries to send per connection", NULL, &config->num_queries_per_conn);
+ perf_long_opt_add("verbose-interval-stats", perf_opt_boolean, NULL,
+ "print detailed statistics for each stats_interval", NULL, &config->verbose_interval_stats);
+#ifdef USE_HISTOGRAMS
+ perf_long_opt_add("latency-histogram", perf_opt_boolean, NULL,
+ "collect and print detailed latency histograms", NULL, &config->latency_histogram);
+#endif
+ perf_long_opt_add("qps-threshold-wait", perf_opt_zpint, "microseconds",
+ "minimum threshold for enabling wait in rate limiting", stringify(config->qps_threshold_wait), &config->qps_threshold_wait);
+ perf_long_opt_add("tls-sni", perf_opt_string, "tls_sni",
+ "the TLS SNI to use for TLS connections", NULL, &tls_sni);
+
+ bool log_stdout = false;
+ perf_opt_add('W', perf_opt_boolean, NULL, "log warnings and errors to stdout instead of stderr", NULL, &log_stdout);
+
+ perf_opt_parse(argc, argv);
+
+ if (log_stdout) {
+ perf_log_tostdout();
+ }
+
+ config->suppress = perf_opt_parse_suppress(local_suppress);
+
+ if (mode != 0)
+ config->mode = perf_net_parsemode(mode);
+
+ if (!server_port) {
+ switch (config->mode) {
+ case sock_doh:
+ server_port = DEFAULT_SERVER_DOH_PORT;
+ break;
+ case sock_dot:
+ server_port = DEFAULT_SERVER_DOT_PORT;
+ break;
+ default:
+ server_port = DEFAULT_SERVER_PORT;
+ break;
+ }
+ }
+
+ if (tls_sni) {
+ perf_net_tls_sni = tls_sni;
+ }
+
+ if (doh_uri) {
+ perf_net_doh_parse_uri(doh_uri);
+ }
+ if (doh_method) {
+ perf_net_doh_parse_method(doh_method);
+ }
+ perf_net_doh_set_max_concurrent_streams(config->max_outstanding);
+
+ if (family != NULL)
+ config->family = perf_net_parsefamily(family);
+ perf_net_parseserver(config->family, server_name, server_port,
+ &config->server_addr);
+ perf_net_parselocal(config->server_addr.sa.sa.sa_family,
+ local_name, local_port, &config->local_addr);
+
+ if (config->binary_input
+ && (config->edns || config->edns_option || config->dnssec
+ || config->tsigkey || config->updates)) {
+ fprintf(stderr, "-B is mutually exclusive with -D, -e, -E, -u, -y\n");
+ exit(1);
+ }
+ if (config->updates)
+ config->input_format = input_format_text_update;
+ else if (config->binary_input)
+ config->input_format = input_format_tcp_wire_format;
+ else
+ config->input_format = input_format_text_query;
+ input = perf_datafile_open(filename, config->input_format);
+
+ if (config->maxruns == 0 && config->timelimit == 0)
+ config->maxruns = 1;
+ perf_datafile_setmaxruns(input, config->maxruns);
+
+ if (config->dnssec || edns_option != NULL)
+ config->edns = true;
+
+ if (config->tsigkey) {
+ // check TSIG key to die earlier than in threads
+ perf_tsigkey_t* k = perf_tsig_parsekey(config->tsigkey);
+ perf_tsig_destroykey(&k);
+ }
+
+ if (edns_option != NULL)
+ config->edns_option = perf_edns_parseoption(edns_option);
+
+ /*
+ * If we run more threads than max-qps, some threads will have
+ * ->max_qps set to 0, and be unlimited.
+ */
+ if (config->max_qps > 0 && config->threads > config->max_qps) {
+ perf_log_warning("requested max QPS limit (-Q %u) is lower than number of threads (-T %u), lowering number of threads", config->max_qps, config->threads);
+ config->threads = config->max_qps;
+ }
+
+ /*
+ * We also can't run more threads than clients.
+ */
+ if (config->threads > config->clients) {
+ perf_log_warning("requested number of threads (-T %u) exceeds number of clients (-c %u), lowering number of threads", config->threads, config->clients);
+ config->threads = config->clients;
+ }
+
+#ifndef HAVE_LDNS
+ if (config->updates) {
+ perf_log_fatal("Unable to dynamic update, support not built in");
+ }
+#endif
+
+ if (config->qps_threshold_wait < 0) {
+ int err = measure_nanosleep(config);
+ if (err) {
+ char __s[256];
+ perf_log_fatal("Unable to measure nanosleep(): %s", perf_strerror_r(errno, __s, sizeof(__s)));
+ }
+ }
+
+#ifdef HAVE_JSON_C
+ if (config->json && config->verbose) {
+ perf_log_fatal("Verbose output (-v) is currently not possible while using JSON output (-j)");
+ }
+ if (config->json && log_stdout) {
+ perf_log_fatal("Logging warnings and errors to stdout (-W) is currently not possible while using JSON output (-j)");
+ }
+#endif
+}
+
+static void
+cleanup(config_t* config)
+{
+ unsigned int i;
+
+ perf_datafile_close(&input);
+ for (i = 0; i < 2; i++) {
+ close(threadpipe[i]);
+ close(mainpipe[i]);
+ close(intrpipe[i]);
+ }
+ if (config->edns_option != NULL)
+ perf_edns_destroyoption(&config->edns_option);
+}
+
+typedef enum {
+ prepend_unused,
+ append_unused,
+ prepend_outstanding,
+} query_move_op;
+
+static inline void
+query_move(threadinfo_t* tinfo, query_info* q, query_move_op op)
+{
+ perf_list_unlink(*q->list, q);
+ switch (op) {
+ case prepend_unused:
+ q->list = &tinfo->unused_queries;
+ perf_list_prepend(tinfo->unused_queries, q);
+ break;
+ case append_unused:
+ q->list = &tinfo->unused_queries;
+ perf_list_append(tinfo->unused_queries, q);
+ break;
+ case prepend_outstanding:
+ q->list = &tinfo->outstanding_queries;
+ perf_list_prepend(tinfo->outstanding_queries, q);
+ break;
+ }
+}
+
+static inline uint64_t
+num_outstanding(const stats_t* stats)
+{
+ /* make sure negative values aren't returned */
+ if (stats->num_completed + stats->num_timedout > stats->num_sent) {
+ return 0;
+ }
+ return stats->num_sent - stats->num_completed - stats->num_timedout;
+}
+
+static void
+wait_for_start(void)
+{
+ PERF_LOCK(&start_lock);
+ while (!started)
+ PERF_WAIT(&start_cond, &start_lock);
+ PERF_UNLOCK(&start_lock);
+}
+
+static inline void
+bit_set(unsigned char* bits, unsigned int bit)
+{
+ unsigned int shift, mask;
+
+ shift = 7 - (bit % 8);
+ mask = 1 << shift;
+
+ bits[bit / 8] |= mask;
+}
+
+static inline void
+bit_clear(unsigned char* bits, unsigned int bit)
+{
+ unsigned int shift, mask;
+
+ shift = 7 - (bit % 8);
+ mask = 1 << shift;
+
+ bits[bit / 8] &= ~mask;
+}
+
+static inline bool
+bit_check(unsigned char* bits, unsigned int bit)
+{
+ unsigned int shift;
+
+ shift = 7 - (bit % 8);
+
+ if ((bits[bit / 8] >> shift) & 0x01)
+ return true;
+ return false;
+}
+
+static void*
+do_send(void* arg)
+{
+ threadinfo_t* tinfo;
+ const config_t* config;
+ const times_t* times;
+ stats_t* stats;
+ unsigned int max_packet_size;
+ perf_buffer_t msg;
+ uint64_t now, req_time, wait_us, q_sent = 0, q_step = 0, q_slice;
+ char input_data[MAX_INPUT_DATA];
+ perf_buffer_t lines;
+ perf_region_t used;
+ query_info* q;
+ int qid;
+ unsigned char packet_buffer[MAX_EDNS_PACKET];
+ unsigned char* base;
+ unsigned int length;
+ int n, i, any_inprogress = 0, sock = 0;
+ perf_result_t result;
+ bool all_fail;
+ unsigned char socketbits[(MAX_SOCKETS / 8) + 1] = {};
+ perf_tsigkey_t* tsigkey = 0;
+
+ tinfo = (threadinfo_t*)arg;
+ config = tinfo->config;
+ times = tinfo->times;
+ stats = &tinfo->stats;
+ max_packet_size = config->edns ? MAX_EDNS_PACKET : MAX_UDP_PACKET;
+ perf_buffer_init(&msg, packet_buffer, max_packet_size);
+ perf_buffer_init(&lines, input_data, sizeof(input_data));
+ if (config->tsigkey) {
+ tsigkey = perf_tsig_parsekey(config->tsigkey);
+ }
+
+ if (tinfo->max_qps > 0) {
+ q_step = MILLION / tinfo->max_qps;
+ }
+ wait_for_start();
+ now = perf_get_time();
+ req_time = now;
+ q_slice = now + MILLION;
+ while (!interrupted && now < times->stop_time) {
+ /* Avoid flooding the network too quickly. */
+ if (stats->num_sent < tinfo->max_outstanding && stats->num_sent % 2 == 1) {
+ if (stats->num_completed == 0)
+ usleep(1000);
+ else
+ sleep(0);
+ now = perf_get_time();
+ }
+
+ /* Some sock might still be sending, try flush all of them */
+ if (any_inprogress) {
+ any_inprogress = 0;
+ for (i = 0; i < tinfo->nsocks; i++) {
+ if (!bit_check(socketbits, i)) {
+ continue;
+ }
+ if (!perf_net_sockready(tinfo->socks[i], threadpipe[0], TIMEOUT_CHECK_TIME)) {
+ any_inprogress = 1;
+ } else {
+ bit_clear(socketbits, i);
+ }
+ }
+ }
+
+ /* Rate limiting */
+ if (tinfo->max_qps > 0) {
+ /* the 1 second time slice where q_sent is calculated over */
+ if (q_slice <= now) {
+ q_slice += MILLION;
+ q_sent = 0;
+ req_time = now; // reset stepping, in case of clock sliding
+ }
+ /* limit QPS over the 1 second slice */
+ if (q_sent >= tinfo->max_qps) {
+ if (!any_inprogress) { // only if nothing is in-progress
+ wait_us = q_slice - now;
+ if (config->qps_threshold_wait && wait_us > config->qps_threshold_wait) {
+ wait_us -= config->qps_threshold_wait;
+ struct timespec ts = { 0, 0 };
+ if (wait_us >= MILLION) {
+ ts.tv_sec = wait_us / MILLION;
+ ts.tv_nsec = (wait_us % MILLION) * 1000;
+ } else {
+ ts.tv_sec = 0;
+ ts.tv_nsec = wait_us * 1000;
+ }
+ nanosleep(&ts, NULL);
+ }
+ }
+ now = perf_get_time();
+ continue;
+ }
+ /* handle stepping to the next window to send a query on */
+ if (req_time > now) {
+ if (!any_inprogress) { // only if nothing is in-progress
+ wait_us = req_time - now;
+ if (config->qps_threshold_wait && wait_us > config->qps_threshold_wait) {
+ wait_us -= config->qps_threshold_wait;
+ struct timespec ts = { 0, 0 };
+ if (wait_us >= MILLION) {
+ ts.tv_sec = wait_us / MILLION;
+ ts.tv_nsec = (wait_us % MILLION) * 1000;
+ } else {
+ ts.tv_sec = 0;
+ ts.tv_nsec = wait_us * 1000;
+ }
+ nanosleep(&ts, NULL);
+ }
+ }
+ now = perf_get_time();
+ continue;
+ }
+ req_time += q_step;
+ }
+
+ PERF_LOCK(&tinfo->lock);
+
+ /* Limit in-flight queries */
+ if (num_outstanding(stats) >= tinfo->max_outstanding) {
+ if (!any_inprogress) { // only if nothing is in-progress
+ PERF_TIMEDWAIT(&tinfo->cond, &tinfo->lock, ×->stop_time_ns, NULL);
+ }
+ PERF_UNLOCK(&tinfo->lock);
+ now = perf_get_time();
+ continue;
+ }
+
+ q = perf_list_head(tinfo->unused_queries);
+ query_move(tinfo, q, prepend_outstanding);
+ q->timestamp = UINT64_MAX;
+
+ i = tinfo->nsocks * 2;
+ all_fail = true;
+ while (i--) {
+ sock = tinfo->current_sock++ % tinfo->nsocks;
+ q->sock = tinfo->socks[sock];
+ switch (perf_net_sockready(q->sock, threadpipe[0], TIMEOUT_CHECK_TIME)) {
+ case 0:
+ if (config->verbose && !config->suppress.sockready) {
+ perf_log_warning("socket %p not ready", q->sock);
+ }
+ q->sock = 0;
+ all_fail = false;
+ continue;
+ case -1:
+ if (config->verbose && !config->suppress.sockready) {
+ perf_log_warning("socket %p readiness check timed out", q->sock);
+ }
+ q->sock = 0;
+ continue;
+ default:
+ break;
+ }
+ all_fail = false;
+ break;
+ };
+
+ if (all_fail) {
+ perf_log_fatal("all sockets reported failure, can not continue");
+ }
+
+ if (!q->sock) {
+ query_move(tinfo, q, prepend_unused);
+ PERF_UNLOCK(&tinfo->lock);
+ now = perf_get_time();
+ continue;
+ }
+ PERF_UNLOCK(&tinfo->lock);
+
+ perf_buffer_clear(&lines);
+ result = perf_datafile_next(input, &lines);
+ if (result != PERF_R_SUCCESS) {
+ if (result == PERF_R_INVALIDFILE)
+ perf_log_fatal("input file contains no data");
+ break;
+ }
+
+ perf_buffer_t* send = &msg;
+ qid = q - tinfo->queries;
+ switch (config->input_format) {
+ case input_format_text_query:
+ case input_format_text_update:
+ perf_buffer_clear(&msg);
+ perf_buffer_usedregion(&lines, &used);
+ result = perf_dns_buildrequest(&used, qid,
+ config->edns, config->dnssec, config->input_format == input_format_text_update,
+ tsigkey, config->edns_option,
+ &msg);
+ break;
+
+ case input_format_tcp_wire_format:
+ send = &lines;
+ if (perf_buffer_usedlength(send) > 1) {
+ ((uint8_t*)perf_buffer_base(send))[0] = qid >> 8;
+ ((uint8_t*)perf_buffer_base(send))[1] = qid;
+ }
+ result = PERF_R_SUCCESS;
+ break;
+ }
+ if (result != PERF_R_SUCCESS) {
+ PERF_LOCK(&tinfo->lock);
+ query_move(tinfo, q, prepend_unused);
+ PERF_UNLOCK(&tinfo->lock);
+ now = perf_get_time();
+ continue;
+ }
+
+ base = perf_buffer_base(send);
+ length = perf_buffer_usedlength(send);
+
+ now = perf_get_time();
+ if (config->verbose) {
+ free(q->desc);
+ if (config->input_format == input_format_tcp_wire_format) {
+ q->desc = strdup("binary input");
+ } else {
+ q->desc = strdup(lines.base);
+ }
+ if (q->desc == NULL)
+ perf_log_fatal("out of memory");
+ }
+ q->timestamp = now;
+
+ n = perf_net_sendto(q->sock, qid, base, length, 0, &config->server_addr.sa.sa,
+ config->server_addr.length);
+ if (n < 0) {
+ if (errno == EINPROGRESS) {
+ if (config->verbose && !config->suppress.congestion) {
+ perf_log_warning("network congested, packet sending in progress");
+ }
+ any_inprogress = 1;
+ bit_set(socketbits, sock);
+ } else {
+ if (config->verbose && !config->suppress.sendfailed) {
+ char __s[256];
+ perf_log_warning("failed to send packet: %s", perf_strerror_r(errno, __s, sizeof(__s)));
+ }
+ PERF_LOCK(&tinfo->lock);
+ query_move(tinfo, q, prepend_unused);
+ PERF_UNLOCK(&tinfo->lock);
+ continue;
+ }
+ } else if ((unsigned int)n != length) {
+ if (!config->suppress.sendfailed) {
+ perf_log_warning("failed to send full packet: only sent %d of %u", n, length);
+ }
+ PERF_LOCK(&tinfo->lock);
+ query_move(tinfo, q, prepend_unused);
+ PERF_UNLOCK(&tinfo->lock);
+ continue;
+ }
+ stats->num_sent++;
+ q_sent++;
+
+ stats->total_request_size += length;
+ }
+
+ while (any_inprogress) {
+ any_inprogress = 0;
+ for (i = 0; i < tinfo->nsocks; i++) {
+ if (!perf_net_sockready(tinfo->socks[i], threadpipe[0], TIMEOUT_CHECK_TIME)) {
+ any_inprogress = 1;
+ }
+ }
+ }
+
+ tinfo->done_send_time = perf_get_time();
+ tinfo->done_sending = true;
+ if (write(mainpipe[1], "", 1)) { // lgtm [cpp/empty-block]
+ }
+
+ if (tsigkey) {
+ perf_tsig_destroykey(&tsigkey);
+ }
+ return NULL;
+}
+
+static void
+process_timeouts(threadinfo_t* tinfo, uint64_t now)
+{
+ struct query_info* q;
+ const config_t* config;
+
+ config = tinfo->config;
+
+ /* Avoid locking unless we need to. */
+ q = perf_list_tail(tinfo->outstanding_queries);
+ if (q == NULL || q->timestamp > now || now - q->timestamp < config->timeout)
+ return;
+
+ PERF_LOCK(&tinfo->lock);
+
+ do {
+ query_move(tinfo, q, append_unused);
+
+ tinfo->stats.num_timedout++;
+
+ if (!config->suppress.timeouts) {
+ if (q->desc != NULL) {
+ perf_log_printf("> T %s", q->desc);
+ } else {
+ perf_log_printf("[Timeout] %s timed out: msg id %u",
+ config->updates ? "Update" : "Query",
+ (unsigned int)(q - tinfo->queries));
+ }
+ }
+ q = perf_list_tail(tinfo->outstanding_queries);
+ } while (q != NULL && q->timestamp < now && now - q->timestamp >= config->timeout);
+
+ PERF_UNLOCK(&tinfo->lock);
+}
+
+typedef struct {
+ struct perf_net_socket* sock;
+ uint16_t qid;
+ uint16_t rcode;
+ unsigned int size;
+ uint64_t when;
+ uint64_t sent;
+ bool unexpected;
+ bool short_response;
+ char* desc;
+} received_query_t;
+
+static bool
+recv_one(threadinfo_t* tinfo, int which_sock,
+ unsigned char* packet_buffer, unsigned int packet_size,
+ received_query_t* recvd, int* saved_errnop)
+{
+ uint16_t* packet_header;
+ uint64_t now;
+ int n;
+
+ packet_header = (uint16_t*)packet_buffer;
+
+ n = perf_net_recv(tinfo->socks[which_sock], packet_buffer, packet_size, 0);
+ now = perf_get_time();
+ if (n < 0) {
+ *saved_errnop = errno;
+ return false;
+ }
+ if (!n) {
+ // Treat connection closed like try again until reconnection features are in
+ if (!*saved_errnop) {
+ // only set this if there was no error before to allow above error check to overwrite EAGAIN
+ *saved_errnop = EAGAIN;
+ }
+ return false;
+ }
+ recvd->sock = tinfo->socks[which_sock];
+ recvd->qid = ntohs(packet_header[0]);
+ recvd->rcode = ntohs(packet_header[1]) & 0xF;
+ recvd->size = n;
+ recvd->when = now;
+ recvd->sent = 0;
+ recvd->unexpected = false;
+ recvd->short_response = (n < 4);
+ recvd->desc = NULL;
+ return true;
+}
+
+static void*
+do_recv(void* arg)
+{
+ threadinfo_t* tinfo;
+ stats_t* stats;
+ unsigned char packet_buffer[MAX_EDNS_PACKET];
+ received_query_t recvd[RECV_BATCH_SIZE] = { { 0, 0, 0, 0, 0, 0, false, false, 0 } };
+ unsigned int nrecvd;
+ int saved_errno;
+ unsigned char socketbits[(MAX_SOCKETS / 8) + 1];
+ uint64_t now, latency;
+ query_info* q;
+ unsigned int current_socket, last_socket;
+ unsigned int i, j;
+
+ tinfo = (threadinfo_t*)arg;
+ stats = &tinfo->stats;
+
+ wait_for_start();
+ now = perf_get_time();
+ last_socket = 0;
+ while (!interrupted) {
+ process_timeouts(tinfo, now);
+
+ /*
+ * If we're done sending and either all responses have been
+ * received, stop.
+ */
+ if (tinfo->done_sending && num_outstanding(stats) == 0)
+ break;
+
+ /*
+ * Try to receive a few packets, so that we can process them
+ * atomically.
+ */
+ saved_errno = 0;
+ memset(socketbits, 0, sizeof(socketbits));
+ for (i = 0; i < RECV_BATCH_SIZE; i++) {
+ for (j = 0; j < tinfo->nsocks; j++) {
+ current_socket = (j + last_socket) % tinfo->nsocks;
+ if (bit_check(socketbits, current_socket))
+ continue;
+ if (recv_one(tinfo, current_socket, packet_buffer,
+ sizeof(packet_buffer), &recvd[i], &saved_errno)) {
+ last_socket = (current_socket + 1);
+ break;
+ }
+ bit_set(socketbits, current_socket);
+ }
+ if (j == tinfo->nsocks)
+ break;
+ }
+ nrecvd = i;
+
+ /* Do all of the processing that requires the lock */
+ PERF_LOCK(&tinfo->lock);
+ for (i = 0; i < nrecvd; i++) {
+ if (recvd[i].short_response)
+ continue;
+
+ q = &tinfo->queries[recvd[i].qid];
+ if (q->list != &tinfo->outstanding_queries || q->timestamp == UINT64_MAX || !perf_net_sockeq(q->sock, recvd[i].sock)) {
+ recvd[i].unexpected = true;
+ continue;
+ }
+ query_move(tinfo, q, append_unused);
+ recvd[i].sent = q->timestamp;
+ recvd[i].desc = q->desc;
+ q->desc = NULL;
+ }
+ PERF_SIGNAL(&tinfo->cond);
+ PERF_UNLOCK(&tinfo->lock);
+
+ /* Now do the rest of the processing unlocked */
+ for (i = 0; i < nrecvd; i++) {
+ if (recvd[i].short_response) {
+ perf_log_warning("received short response");
+ continue;
+ }
+ if (recvd[i].unexpected) {
+ if (!tinfo->config->suppress.unexpected) {
+ perf_log_warning("received a response with an "
+ "unexpected (maybe timed out) "
+ "id: %u",
+ recvd[i].qid);
+ }
+ stats->num_unexpected++;
+ continue;
+ }
+ latency = recvd[i].when - recvd[i].sent;
+ if (recvd[i].desc != NULL) {
+ perf_log_printf(
+ "> %s %s %u.%06u",
+ perf_dns_rcode_strings[recvd[i].rcode],
+ recvd[i].desc,
+ (unsigned int)(latency / MILLION),
+ (unsigned int)(latency % MILLION));
+ free(recvd[i].desc);
+ }
+
+ stats->num_completed++;
+ stats->total_response_size += recvd[i].size;
+ stats->rcodecounts[recvd[i].rcode]++;
+ stats->latency_sum += latency;
+#ifdef USE_HISTOGRAMS
+ if (stats->latency) {
+ hg64_inc(stats->latency, latency);
+ }
+#endif
+ stats->latency_sum_squares += (latency * latency);
+ if (latency < stats->latency_min || stats->num_completed == 1)
+ stats->latency_min = latency;
+ if (latency > stats->latency_max)
+ stats->latency_max = latency;
+ }
+
+ if (nrecvd > 0)
+ tinfo->last_recv = recvd[nrecvd - 1].when;
+
+ /*
+ * If there was an error, handle it (by either ignoring it,
+ * blocking, or exiting).
+ */
+ if (nrecvd < RECV_BATCH_SIZE) {
+ if (saved_errno == EINTR) {
+ continue;
+ } else if (saved_errno == EAGAIN) {
+ perf_os_waituntilanyreadable(tinfo->socks, tinfo->nsocks,
+ threadpipe[0], TIMEOUT_CHECK_TIME);
+ now = perf_get_time();
+ continue;
+ } else {
+ char __s[256];
+ perf_log_fatal("failed to receive packet: %s", perf_strerror_r(saved_errno, __s, sizeof(__s)));
+ }
+ }
+ }
+
+ return NULL;
+}
+
+static void*
+do_interval_stats(void* arg)
+{
+ threadinfo_t* tinfo;
+ stats_t total = {};
+ stats_t last = {};
+ stats_t diff = {};
+ uint64_t now;
+ uint64_t last_interval_time;
+ uint64_t interval_time;
+ double qps;
+ struct perf_net_socket sock = { .mode = sock_pipe, .fd = threadpipe[0] };
+
+ tinfo = arg;
+ last_interval_time = tinfo->times->start_time;
+
+ wait_for_start();
+ while (perf_os_waituntilreadable(&sock, threadpipe[0],
+ tinfo->config->stats_interval)
+ == PERF_R_TIMEDOUT) {
+ now = perf_get_time();
+ sum_stats(tinfo->config, &total);
+ interval_time = now - last_interval_time;
+
+ if (tinfo->config->verbose_interval_stats) {
+ diff_stats(tinfo->config, &last, &total, &diff);
+ print_statistics(tinfo->config, tinfo->times, &diff, now, interval_time);
+ } else {
+ qps = (total.num_completed - last.num_completed) / (((double)interval_time) / MILLION);
+#ifdef HAVE_JSON_C
+ if (tinfo->config->json) {
+ int ret = 0;
+
+ struct json_object* rate = json_object_new_object();
+
+ ret |= json_object_object_add(rate, "timestamp_usec", json_object_new_uint64(now));
+ ret |= json_object_object_add(rate, "qps", json_object_new_double(qps));
+
+ struct json_object* json = json_object_new_object();
+ ret |= json_object_object_add(json, "rate", rate);
+ if (!ret) {
+ printf("%s\n", json_object_to_json_string_ext(json, JSON_C_TO_STRING_NOSLASHESCAPE));
+ } else {
+ perf_log_fatal("unable to construct JSON output");
+ }
+ json_object_put(json);
+ } else
+#endif
+ {
+ perf_log_printf("%u.%06u: %.6lf",
+ (unsigned int)(now / MILLION),
+ (unsigned int)(now % MILLION), qps);
+ }
+ }
+
+ last_interval_time = now;
+#ifdef USE_HISTOGRAMS
+ free(last.latency);
+ free(last.conn_latency);
+#endif
+ last = total;
+ }
+
+ return NULL;
+}
+
+static void
+cancel_queries(threadinfo_t* tinfo)
+{
+ struct query_info* q;
+
+ while (true) {
+ q = perf_list_tail(tinfo->outstanding_queries);
+ if (q == NULL)
+ break;
+ query_move(tinfo, q, append_unused);
+
+ if (q->timestamp == UINT64_MAX)
+ continue;
+
+ tinfo->stats.num_interrupted++;
+ if (q->desc != NULL) {
+ perf_log_printf("> I %s", q->desc);
+ free(q->desc);
+ q->desc = NULL;
+ }
+ }
+}
+
+static uint32_t
+per_thread(uint32_t total, uint32_t nthreads, unsigned int offset)
+{
+ uint32_t value, temp_total;
+
+ value = total / nthreads;
+
+ /*
+ * work out if there's a shortfall and adjust if necessary
+ */
+ temp_total = value * nthreads;
+ if (temp_total < total && offset < total - temp_total)
+ value++;
+
+ return value;
+}
+
+static void perf__net_sent(struct perf_net_socket* sock, uint16_t qid)
+{
+ threadinfo_t* tinfo = (threadinfo_t*)sock->data;
+ query_info* q;
+
+ q = &tinfo->queries[qid];
+ if (q->timestamp != UINT64_MAX) {
+ q->timestamp = perf_get_time();
+ }
+}
+
+static void perf__net_event(struct perf_net_socket* sock, perf_socket_event_t event, uint64_t elapsed_time)
+{
+ stats_t* stats = &((threadinfo_t*)sock->data)->stats;
+
+ switch (event) {
+ case perf_socket_event_reconnected:
+ case perf_socket_event_connected:
+ stats->num_conn_completed++;
+
+#ifdef USE_HISTOGRAMS
+ if (stats->conn_latency) {
+ hg64_inc(stats->conn_latency, elapsed_time);
+ }
+#endif
+ stats->conn_latency_sum += elapsed_time;
+ stats->conn_latency_sum_squares += (elapsed_time * elapsed_time);
+ if (elapsed_time < stats->conn_latency_min || stats->num_conn_completed == 1)
+ stats->conn_latency_min = elapsed_time;
+ if (elapsed_time > stats->conn_latency_max)
+ stats->conn_latency_max = elapsed_time;
+ break;
+
+ case perf_socket_event_reconnecting:
+ case perf_socket_event_connecting:
+ stats->num_conn_attempts++;
+ break;
+
+ default:
+ break;
+ }
+}
+
+static void
+threadinfo_init(threadinfo_t* tinfo, const config_t* config,
+ const times_t* times, int idx)
+{
+ unsigned int offset, socket_offset, i;
+
+ memset(tinfo, 0, sizeof(*tinfo));
+ PERF_MUTEX_INIT(&tinfo->lock);
+ PERF_COND_INIT(&tinfo->cond);
+
+ perf_list_init(tinfo->outstanding_queries);
+ perf_list_init(tinfo->unused_queries);
+#ifdef USE_HISTOGRAMS
+ if (config->latency_histogram) {
+ tinfo->stats.latency = hg64_create(HISTOGRAM_SIGBITS);
+ tinfo->stats.conn_latency = hg64_create(HISTOGRAM_SIGBITS);
+ }
+#endif
+ for (i = 0; i < NQIDS; i++) {
+ perf_link_init(&tinfo->queries[i]);
+ perf_list_append(tinfo->unused_queries, &tinfo->queries[i]);
+ tinfo->queries[i].list = &tinfo->unused_queries;
+ }
+
+ offset = tinfo - threads;
+
+ tinfo->config = config;
+ tinfo->times = times;
+
+ /*
+ * Compute per-thread limits based on global values.
+ */
+ tinfo->max_outstanding = per_thread(config->max_outstanding,
+ config->threads, offset);
+ tinfo->max_qps = per_thread(config->max_qps, config->threads, offset);
+ tinfo->nsocks = per_thread(config->clients, config->threads, offset);
+
+ /*
+ * We can't have more than 64k outstanding queries per thread.
+ */
+ if (tinfo->max_outstanding > NQIDS) {
+ perf_log_warning("requested number of outstanding queries (-q %u) per single thread (-T) exceeds built-in maximum %u, adjusting", tinfo->max_outstanding, NQIDS);
+ tinfo->max_outstanding = NQIDS;
+ }
+
+ if (tinfo->nsocks > MAX_SOCKETS) {
+ perf_log_warning("requested number of clients (-c %u) per thread (-T) exceeds built-in maximum %u, adjusting", tinfo->nsocks, MAX_SOCKETS);
+ tinfo->nsocks = MAX_SOCKETS;
+ }
+
+ if (!(tinfo->socks = calloc(tinfo->nsocks, sizeof(*tinfo->socks)))) {
+ perf_log_fatal("out of memory");
+ }
+ socket_offset = 0;
+ for (i = 0; i < offset; i++)
+ socket_offset += threads[i].nsocks;
+ for (i = 0; i < tinfo->nsocks; i++) {
+ tinfo->socks[i] = perf_net_opensocket(config->mode, &config->server_addr,
+ &config->local_addr,
+ socket_offset++,
+ config->bufsize,
+ tinfo, perf__net_sent, perf__net_event);
+ if (!tinfo->socks[i]) {
+ perf_log_fatal("perf_net_opensocket(): no socket returned, out of memory?");
+ }
+ if (config->num_queries_per_conn && tinfo->socks[i]->num_queries_per_conn) {
+ tinfo->socks[i]->num_queries_per_conn(tinfo->socks[i], config->num_queries_per_conn, config->timeout);
+ }
+ }
+ tinfo->current_sock = 0;
+
+ char name[16]; // glibc is limited to 16 characters
+ PERF_THREAD(&tinfo->receiver, do_recv, tinfo);
+ snprintf(name, sizeof(name), "perf-recv-%04d", idx);
+ perf_os_thread_setname(tinfo->receiver, name);
+ PERF_THREAD(&tinfo->sender, do_send, tinfo);
+ snprintf(name, sizeof(name), "perf-send-%04d", idx);
+ perf_os_thread_setname(tinfo->sender, name);
+}
+
+static void
+threadinfo_stop(threadinfo_t* tinfo)
+{
+ PERF_SIGNAL(&tinfo->cond);
+ PERF_JOIN(tinfo->sender, NULL);
+ PERF_JOIN(tinfo->receiver, NULL);
+}
+
+static void
+threadinfo_cleanup(config_t* config, threadinfo_t* tinfo, times_t* times)
+{
+ unsigned int i;
+
+ if (interrupted)
+ cancel_queries(tinfo);
+ for (i = 0; i < tinfo->nsocks; i++) {
+ perf_net_stats_compile(config->mode, tinfo->socks[i]);
+ perf_net_close(tinfo->socks[i]);
+ }
+ if (tinfo->last_recv > times->end_time)
+ times->end_time = tinfo->last_recv;
+}
+
+int main(int argc, char** argv)
+{
+ config_t config;
+ times_t times;
+ stats_t total_stats;
+ threadinfo_t stats_thread;
+ unsigned int i;
+ perf_result_t result;
+ struct perf_net_socket sock = { .mode = sock_pipe };
+
+ (void)SSL_library_init();
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
+ SSL_load_error_strings();
+ OPENSSL_config(0);
+#endif
+
+ setup(argc, argv, &config);
+
+#ifdef HAVE_JSON_C
+ if (!config.json)
+#endif
+ printf("DNS Performance Testing Tool\n"
+ "Version " PACKAGE_VERSION "\n\n");
+
+ if (pipe(threadpipe) < 0 || pipe(mainpipe) < 0 || pipe(intrpipe) < 0)
+ perf_log_fatal("creating pipe");
+
+ perf_datafile_setpipefd(input, threadpipe[0]);
+
+ perf_os_blocksignal(SIGINT, true);
+ switch (config.mode) {
+ case sock_tcp:
+ case sock_dot:
+ case sock_doh:
+ // block SIGPIPE for TCP/DOT mode, if connection is closed it will generate a signal
+ perf_os_blocksignal(SIGPIPE, true);
+ break;
+ default:
+ break;
+ }
+
+ print_initial_status(&config);
+
+ if (!(threads = calloc(config.threads, sizeof(threadinfo_t)))) {
+ perf_log_fatal("out of memory");
+ }
+ for (i = 0; i < config.threads; i++)
+ threadinfo_init(&threads[i], &config, ×, i);
+ if (config.stats_interval > 0) {
+ stats_thread.config = &config;
+ stats_thread.times = ×
+ PERF_THREAD(&stats_thread.sender, do_interval_stats, &stats_thread);
+ }
+
+ times.start_time = perf_get_time();
+ if (config.timelimit > 0)
+ times.stop_time = times.start_time + config.timelimit;
+ else
+ times.stop_time = UINT64_MAX;
+ times.stop_time_ns.tv_sec = times.stop_time / MILLION;
+ times.stop_time_ns.tv_nsec = (times.stop_time % MILLION) * 1000;
+
+ PERF_LOCK(&start_lock);
+ started = true;
+ PERF_BROADCAST(&start_cond);
+ PERF_UNLOCK(&start_lock);
+
+ perf_os_handlesignal(SIGINT, handle_sigint);
+ perf_os_blocksignal(SIGINT, false);
+ sock.fd = mainpipe[0];
+ result = perf_os_waituntilreadable(&sock, intrpipe[0], times.stop_time - times.start_time);
+ if (result == PERF_R_CANCELED)
+ interrupted = true;
+
+ times.end_time = perf_get_time();
+
+ if (write(threadpipe[1], "", 1)) { // lgtm [cpp/empty-block]
+ }
+ for (i = 0; i < config.threads; i++)
+ threadinfo_stop(&threads[i]);
+ if (config.stats_interval > 0)
+ PERF_JOIN(stats_thread.sender, NULL);
+
+ perf_net_stats_init(config.mode);
+
+ for (i = 0; i < config.threads; i++)
+ threadinfo_cleanup(&config, &threads[i], ×);
+
+ print_final_status(&config);
+
+ sum_stats(&config, &total_stats);
+ print_statistics(&config, ×, &total_stats, 0, 0);
+#ifdef HAVE_JSON_C
+ perf_net_stats_print(config.mode, config.json);
+#else
+ perf_net_stats_print(config.mode, false);
+#endif
+
+ cleanup(&config);
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
+ ERR_free_strings();
+#endif
+
+ return (0);
+}
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "config.h"
+
+#include "edns.h"
+
+#include "log.h"
+#include "opt.h"
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <limits.h>
+#include <string.h>
+
+#define EDNSLEN 11
+
+perf_ednsoption_t* perf_edns_parseoption(const char* arg)
+{
+ char * copy, *sep, *value, *endptr, hex[3];
+ perf_ednsoption_t* option;
+ size_t data_len;
+ unsigned long int u;
+ perf_buffer_t save;
+
+ copy = strdup(arg);
+ if (!copy) {
+ perf_log_fatal("out of memory");
+ return 0; // fix clang scan-build
+ }
+
+ sep = strchr(copy, ':');
+ if (!sep) {
+ perf_log_warning("invalid EDNS Option, must be code:value");
+ perf_opt_usage();
+ exit(1);
+ }
+ *sep = '\0';
+ value = sep + 1;
+
+ data_len = strlen(value);
+ if (!data_len) {
+ perf_log_warning("invalid EDNS Option, value is empty");
+ perf_opt_usage();
+ exit(1);
+ }
+ if (data_len & 1) {
+ perf_log_warning("invalid EDNS Option, value must hex string (even number of characters)");
+ perf_opt_usage();
+ exit(1);
+ }
+ data_len /= 2;
+ data_len += 4; // code, len, data...
+
+ option = calloc(1, sizeof(perf_ednsoption_t) + data_len);
+ if (!option) {
+ perf_log_fatal("out of memory");
+ free(copy); // fix clang scan-build
+ return 0; // fix clang scan-build
+ }
+ perf_buffer_init(&option->buffer, &option->data[0], data_len);
+
+ endptr = 0;
+ u = strtoul(copy, &endptr, 10);
+ if (*endptr || u == ULONG_MAX) {
+ perf_log_warning("invalid EDNS Option code '%s'", copy);
+ perf_opt_usage();
+ exit(1);
+ }
+ perf_buffer_putuint16(&option->buffer, u & 0xffff);
+
+ save = option->buffer;
+ perf_buffer_add(&option->buffer, 2);
+ hex[2] = 0;
+ while (*value) {
+ memcpy(hex, value, 2);
+ endptr = 0;
+ u = strtoul(hex, &endptr, 16);
+ if (*endptr || u == ULONG_MAX) {
+ perf_log_warning("invalid EDNS Option hex value '%.*s'", 2, value);
+ perf_opt_usage();
+ exit(1);
+ }
+ perf_buffer_putuint8(&option->buffer, u & 0xff);
+ value += 2;
+ }
+ perf_buffer_putuint16(&save, perf_buffer_usedlength(&option->buffer) - 4);
+
+ free(copy);
+
+ return option;
+}
+
+void perf_edns_destroyoption(perf_ednsoption_t** optionp)
+{
+ assert(optionp);
+ assert(*optionp);
+
+ free(*optionp);
+ *optionp = 0;
+}
+
+/*
+ * Appends an OPT record to the packet.
+ */
+perf_result_t perf_add_edns(perf_buffer_t* packet, bool dnssec, perf_ednsoption_t* option)
+{
+ unsigned char* base;
+ size_t option_length = 0, total_length;
+
+ if (option) {
+ option_length = perf_buffer_usedlength(&option->buffer);
+ }
+ total_length = EDNSLEN + option_length;
+
+ if (perf_buffer_availablelength(packet) < total_length) {
+ perf_log_warning("failed to add OPT to query packet");
+ return PERF_R_NOSPACE;
+ }
+
+ base = perf_buffer_base(packet);
+
+ perf_buffer_putuint8(packet, 0); /* root name */
+ perf_buffer_putuint16(packet, 41); /* OPT record */
+ perf_buffer_putuint16(packet, MAX_EDNS_PACKET); /* class */
+ perf_buffer_putuint8(packet, 0); /* xrcode */
+ perf_buffer_putuint8(packet, 0); /* version */
+ if (dnssec) {
+ /* flags */
+ perf_buffer_putuint16(packet, 0x8000);
+ } else {
+ perf_buffer_putuint16(packet, 0);
+ }
+ perf_buffer_putuint16(packet, option_length); /* rdlen */
+ if (option) {
+ perf_buffer_putmem(packet, perf_buffer_base(&option->buffer), option_length);
+ }
+
+ base[11]++; /* increment additional record count */
+
+ return PERF_R_SUCCESS;
+}
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "result.h"
+#include "buffer.h"
+
+#ifndef PERF_EDNS_H
+#define PERF_EDNS_H 1
+
+#include <stdbool.h>
+
+#define MAX_EDNS_PACKET 4096
+
+typedef struct perf_ednsoption {
+ perf_buffer_t buffer;
+ char data[];
+} perf_ednsoption_t;
+
+perf_ednsoption_t* perf_edns_parseoption(const char* arg);
+
+void perf_edns_destroyoption(perf_ednsoption_t** optionp);
+
+perf_result_t perf_add_edns(perf_buffer_t* packet, bool dnssec, perf_ednsoption_t* option);
+
+#endif
--- /dev/null
+/*
+ * hg64 - 64-bit histograms
+ *
+ * Written by Tony Finch <dot@dotat.at> <fanf@isc.org>
+ *
+ * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
+ *
+ * SPDX-License-Identifier: MPL-2.0
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, you can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <assert.h>
+#include <errno.h>
+#include <stdatomic.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "hg64.h"
+
+/* number of bins is same as number of bits in a value */
+#define BINS 64
+
+typedef atomic_uint_fast64_t counter;
+typedef _Atomic(counter*) bin_ptr;
+
+struct hg64 {
+ unsigned sigbits;
+ bin_ptr bin[BINS];
+};
+
+static inline counter*
+get_bin(hg64* hg, unsigned b)
+{
+ /* key_to_new_counter() below has the matching store / release */
+ return (atomic_load_explicit(&hg->bin[b], memory_order_acquire));
+}
+
+/*
+ * when we only care about the histogram precision
+ */
+struct hg64p {
+ unsigned sigbits;
+};
+
+#ifdef __has_attribute
+#if __has_attribute(__transparent_union__)
+#define TRANSPARENT __attribute__((__transparent_union__))
+#endif
+#endif
+
+#ifdef TRANSPARENT
+
+typedef union hg64u {
+ hg64* hg;
+ const struct hg64p* hp;
+} hg64u TRANSPARENT;
+
+#define hg64p(hu) ((hu).hp)
+#else
+
+typedef void* hg64u;
+
+#define hg64p(hu) ((const struct hg64p*)(hu))
+#endif
+
+/*
+ * The bins arrays have a static size for simplicity, but that means We
+ * waste a little extra space that could be saved by omitting the
+ * exponents that land in the denormal number bin. The following macros
+ * calculate (at run time) the exact number of keys when we need to do
+ * accurate bounds checks.
+ */
+#define DENORMALS(hp) ((hp)->sigbits - 1)
+#define EXPONENTS(hp) (BINS - DENORMALS(hp))
+#define MANTISSAS(hp) (1 << (hp)->sigbits)
+#define KEYS(hp) (EXPONENTS(hp) * MANTISSAS(hp))
+
+#define BINSIZE(hp) MANTISSAS(hp)
+
+/**********************************************************************/
+
+#define OUTARG(ptr, val) (void)(((ptr) != NULL) && (bool)(*(ptr) = (val)))
+
+/**********************************************************************/
+
+hg64* hg64_create(unsigned sigbits)
+{
+ if (sigbits < 1 || 15 < sigbits) {
+ return (NULL);
+ }
+ hg64* hg = malloc(sizeof(*hg));
+ hg->sigbits = sigbits;
+ /*
+ * it is probably portable to zero-initialize atomics but the
+ * C standard says we shouldn't rely on it; but this loop
+ * should optimize to memset() on most target systems
+ */
+ for (unsigned b = 0; b < BINS; b++) {
+ atomic_init(&hg->bin[b], NULL);
+ }
+ return (hg);
+}
+
+void hg64_destroy(hg64* hg)
+{
+ for (unsigned b = 0; b < BINS; b++) {
+ free(get_bin(hg, b));
+ }
+ *hg = (hg64) { 0 };
+ free(hg);
+}
+
+/**********************************************************************/
+
+static inline uint64_t
+key_to_minval(hg64u hu, unsigned key)
+{
+ unsigned binsize = BINSIZE(hg64p(hu));
+ unsigned exponent = (key / binsize) - 1;
+ uint64_t mantissa = (key % binsize) + binsize;
+ return (key < binsize ? key : mantissa << exponent);
+}
+
+/*
+ * don't shift by 64, and don't underflow exponent; instead,
+ * reduce shift by 1 for each hazard and pre-shift UINT64_MAX
+ */
+static inline uint64_t
+key_to_maxval(hg64u hu, unsigned key)
+{
+ unsigned binsize = BINSIZE(hg64p(hu));
+ unsigned shift = 63 - (key / binsize);
+ uint64_t range = UINT64_MAX / 4 >> shift;
+ return (key_to_minval(hu, key) + range);
+}
+
+/*
+ * This branchless conversion is due to Paul Khuong: see bin_down_of() in
+ * https://pvk.ca/Blog/2015/06/27/linear-log-bucketing-fast-versatile-simple/
+ */
+static inline unsigned
+value_to_key(hg64u hu, uint64_t value)
+{
+ /* fast path */
+ const struct hg64p* hp = hg64p(hu);
+ /* ensure that denormal numbers are all in the same bin */
+ uint64_t binned = value | BINSIZE(hp);
+ int clz = __builtin_clzll((unsigned long long)(binned));
+ /* actually 1 less than the exponent except for denormals */
+ unsigned exponent = 63 - hp->sigbits - clz;
+ /* mantissa has leading bit set except for denormals */
+ unsigned mantissa = value >> exponent;
+ /* leading bit of mantissa adds one to exponent */
+ return ((exponent << hp->sigbits) + mantissa);
+}
+
+static counter*
+key_to_new_counter(hg64* hg, unsigned key)
+{
+ /* slow path */
+ unsigned binsize = BINSIZE(hg);
+ unsigned b = key / binsize;
+ unsigned c = key % binsize;
+ counter* old_bp = NULL;
+ counter* new_bp = malloc(sizeof(counter) * binsize);
+ /* see comment in hg64_create() above */
+ for (unsigned i = 0; i < binsize; i++) {
+ atomic_init(new_bp + i, 0);
+ }
+ bin_ptr* bpp = &hg->bin[b];
+ if (atomic_compare_exchange_strong_explicit(bpp, &old_bp, new_bp,
+ memory_order_acq_rel, memory_order_acquire)) {
+ return (new_bp + c);
+ } else {
+ /* lost the race, so use the winner's counters */
+ free(new_bp);
+ return (old_bp + c);
+ }
+}
+
+static inline counter*
+key_to_counter(hg64* hg, unsigned key)
+{
+ /* fast path */
+ unsigned binsize = BINSIZE(hg);
+ unsigned b = key / binsize;
+ unsigned c = key % binsize;
+ counter* bp = get_bin(hg, b);
+ return (bp == NULL ? NULL : bp + c);
+}
+
+static inline uint64_t
+get_key_count(hg64* hg, unsigned key)
+{
+ counter* ctr = key_to_counter(hg, key);
+ return (ctr == NULL ? 0 : atomic_load_explicit(ctr, memory_order_relaxed));
+}
+
+static inline void
+add_key_count(hg64* hg, unsigned key, uint64_t inc)
+{
+ if (inc == 0)
+ return;
+ counter* ctr = key_to_counter(hg, key);
+ ctr = ctr ? ctr : key_to_new_counter(hg, key);
+ atomic_fetch_add_explicit(ctr, inc, memory_order_relaxed);
+}
+
+/**********************************************************************/
+
+void hg64_inc(hg64* hg, uint64_t value)
+{
+ add_key_count(hg, value_to_key(hg, value), 1);
+}
+
+bool hg64_get(hg64* hg, unsigned key,
+ uint64_t* pmin, uint64_t* pmax, uint64_t* pcount)
+{
+ if (key < KEYS(hg)) {
+ OUTARG(pmin, key_to_minval(hg, key));
+ OUTARG(pmax, key_to_maxval(hg, key));
+ OUTARG(pcount, get_key_count(hg, key));
+ return (true);
+ } else {
+ return (false);
+ }
+}
+
+unsigned
+hg64_next(hg64* hg, unsigned key)
+{
+ key++;
+ while (key < KEYS(hg) && (key & (BINSIZE(hg) - 1)) == 0 && key_to_counter(hg, key) == NULL) {
+ key += BINSIZE(hg);
+ }
+ return (key);
+}
+
+/*
+ * https://fanf2.user.srcf.net/hermes/doc/antiforgery/stats.pdf
+ */
+void hg64_mean_variance(hg64* hg, double* pmean, double* pvar)
+{
+ double pop = 0.0;
+ double mean = 0.0;
+ double sigma = 0.0;
+ uint64_t min, max, count;
+ for (unsigned key = 0;
+ hg64_get(hg, key, &min, &max, &count);
+ key = hg64_next(hg, key)) {
+ double delta = (double)min / 2.0 + (double)max / 2.0 - mean;
+ if (count != 0) { /* avoid division by zero */
+ pop += count;
+ mean += count * delta / pop;
+ sigma += count * delta * (min + max - mean);
+ }
+ }
+ OUTARG(pmean, mean);
+ OUTARG(pvar, sigma / pop);
+}
+
+/**********************************************************************/
+
+void hg64_merge(hg64* target, hg64* source)
+{
+ uint64_t count;
+ for (unsigned skey = 0;
+ hg64_get(source, skey, NULL, NULL, &count);
+ skey = hg64_next(source, skey)) {
+ uint64_t svmin = key_to_minval(source, skey);
+ uint64_t svmax = key_to_maxval(source, skey);
+ unsigned tkmin = value_to_key(target, svmin);
+ unsigned tkmax = value_to_key(target, svmax);
+ unsigned keys = tkmax - tkmin + 1;
+ /* is there a more cunning way to spread out the remainder? */
+ uint64_t div = count / keys;
+ uint64_t rem = count % keys;
+ for (unsigned tkey = tkmin; tkey <= tkmax; tkey++) {
+ uint64_t inc = div + (uint64_t)(tkey < rem);
+ add_key_count(target, tkey, inc);
+ }
+ }
+}
+
+void hg64_diff(hg64* a, hg64* b, hg64* diff)
+{
+ assert((a->sigbits == b->sigbits) && (b->sigbits == diff->sigbits));
+ uint64_t count_a = 0;
+ uint64_t count_b = 0;
+ for (unsigned key = 0;
+ hg64_get(a, key, NULL, NULL, &count_a);
+ key++) {
+ hg64_get(b, key, NULL, NULL, &count_b);
+ add_key_count(diff, key, count_a - count_b);
+ }
+}
+
+unsigned hg64_min_key(hg64* hg)
+{
+ uint64_t pcount;
+ for (unsigned key = 0;
+ hg64_get(hg, key, NULL, NULL, &pcount);
+ key = hg64_next(hg, key)) {
+ if (pcount > 0)
+ return key;
+ }
+ return 0;
+}
+
+unsigned hg64_max_key(hg64* hg)
+{
+ unsigned last_key = 0;
+ uint64_t pcount;
+ for (unsigned key = 0;
+ hg64_get(hg, key, NULL, NULL, &pcount);
+ key = hg64_next(hg, key)) {
+ if (pcount > 0)
+ last_key = key;
+ }
+ return last_key;
+}
--- /dev/null
+/*
+ * hg64 - 64-bit histograms
+ *
+ * Written by Tony Finch <dot@dotat.at> <fanf@isc.org>
+ *
+ * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
+ *
+ * SPDX-License-Identifier: MPL-2.0
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, you can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#ifndef HG64_H
+#define HG64_H 1
+
+typedef struct hg64 hg64;
+
+/*
+ * Allocate a new histogram. `sigbits` must be between 1 and 15
+ * inclusive; it is the number of significant bits of each value
+ * to use when mapping values to buckets.
+ */
+hg64* hg64_create(unsigned sigbits);
+
+/*
+ * Free the memory used by a histogram
+ */
+void hg64_destroy(hg64* hg);
+
+/*
+ * Add 1 to the value's bucket
+ */
+void hg64_inc(hg64* hg, uint64_t value);
+
+/*
+ * Get information about a bucket. This can be used as an iterator,
+ * by initializing `key` to zero and incrementing by one or using
+ * `hg64_next()` until `hg64_get()` returns `false`. The number of
+ * iterations is a little less than `1 << (6 + sigbits)`.
+ *
+ * If `pmin` is non-NULL it is set to the bucket's minimum inclusive value.
+ *
+ * If `pmax` is non-NULL it is set to the bucket's maximum inclusive value.
+ *
+ * If `pcount` is non-NULL it is set to the bucket's counter, which
+ * can be zero. (Empty buckets are included in the iterator.)
+ */
+bool hg64_get(hg64* hg, unsigned key,
+ uint64_t* pmin, uint64_t* pmax, uint64_t* pcount);
+
+/*
+ * Skip to the next key, omitting groups of nonexistent buckets.
+ */
+unsigned hg64_next(hg64* hg, unsigned key);
+
+/*
+ * Get summary statistics about the histogram.
+ *
+ * If `pmean` is non-NULL it is set to the mean of the recorded data.
+ *
+ * If `pvar` is non-NULL it is set to the variance of the recorded
+ * data. The standard deviation is the square root of the variance.
+ */
+void hg64_mean_variance(hg64* hg, double* pmean, double* pvar);
+
+/*
+ * Increase the counts in `target` by the counts recorded in `source`
+ */
+void hg64_merge(hg64* target, hg64* source);
+
+/*
+ * diff = a - b
+ */
+void hg64_diff(hg64* a, hg64* b, hg64* diff);
+
+/*
+ * Get highest key with non-zero value. Returns 0 if all values are 0.
+ */
+unsigned hg64_max_key(hg64* hg);
+
+/*
+ * Get lowest key with non-zero value. Returns 0 if all values are 0.
+ */
+unsigned hg64_min_key(hg64* hg);
+
+#endif
--- /dev/null
+#include "parse_uri.h"
+
+#include <string.h>
+
+// From: https://github.com/nghttp2/nghttp2/blob/master/examples/client.c
+/*
+ * nghttp2 - HTTP/2 C Library
+ *
+ * Copyright (c) 2013 Tatsuhiro Tsujikawa
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+int parse_uri(struct URI* res, const char* uri)
+{
+ /* We only interested in https */
+ size_t len, i, offset;
+ int ipv6addr = 0;
+ memset(res, 0, sizeof(struct URI));
+ len = strlen(uri);
+ if (len < 9 || memcmp("https://", uri, 8) != 0) {
+ return -1;
+ }
+ offset = 8;
+ res->host = res->hostport = &uri[offset];
+ res->hostlen = 0;
+ if (uri[offset] == '[') {
+ /* IPv6 literal address */
+ ++offset;
+ ++res->host;
+ ipv6addr = 1;
+ for (i = offset; i < len; ++i) {
+ if (uri[i] == ']') {
+ res->hostlen = i - offset;
+ offset = i + 1;
+ break;
+ }
+ }
+ } else {
+ const char delims[] = ":/?#";
+ for (i = offset; i < len; ++i) {
+ if (strchr(delims, uri[i]) != NULL) {
+ break;
+ }
+ }
+ res->hostlen = i - offset;
+ offset = i;
+ }
+ if (res->hostlen == 0) {
+ return -1;
+ }
+ /* Assuming https */
+ res->port = 443;
+ if (offset < len) {
+ if (uri[offset] == ':') {
+ /* port */
+ const char delims[] = "/?#";
+ int port = 0;
+ ++offset;
+ for (i = offset; i < len; ++i) {
+ if (strchr(delims, uri[i]) != NULL) {
+ break;
+ }
+ if ('0' <= uri[i] && uri[i] <= '9') {
+ port *= 10;
+ port += uri[i] - '0';
+ if (port > 65535) {
+ return -1;
+ }
+ } else {
+ return -1;
+ }
+ }
+ if (port == 0) {
+ return -1;
+ }
+ offset = i;
+ res->port = (uint16_t)port;
+ }
+ }
+ res->hostportlen = (size_t)(uri + offset + ipv6addr - res->host);
+ for (i = offset; i < len; ++i) {
+ if (uri[i] == '#') {
+ break;
+ }
+ }
+ if (i - offset == 0) {
+ res->path = "/";
+ res->pathlen = 1;
+ } else {
+ res->path = &uri[offset];
+ res->pathlen = i - offset;
+ }
+ return 0;
+}
--- /dev/null
+// From: https://github.com/nghttp2/nghttp2/blob/master/examples/client.c
+/*
+ * nghttp2 - HTTP/2 C Library
+ *
+ * Copyright (c) 2013 Tatsuhiro Tsujikawa
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <inttypes.h>
+#include <stdlib.h>
+
+struct URI {
+ const char* host;
+ /* In this program, path contains query component as well. */
+ const char* path;
+ size_t pathlen;
+ const char* hostport;
+ size_t hostlen;
+ size_t hostportlen;
+ uint16_t port;
+};
+
+int parse_uri(struct URI* res, const char* uri);
--- /dev/null
+#!/usr/bin/python3
+
+import csv
+from urllib.request import Request, urlopen
+from io import StringIO
+
+qtype = {}
+
+for row in csv.reader(StringIO(urlopen(Request('https://www.iana.org/assignments/dns-parameters/dns-parameters-4.csv')).read().decode('utf-8'))):
+ if row[0] == 'TYPE':
+ continue
+ try:
+ qtype[row[0]] = int(row[1])
+ except Exception:
+ continue
+
+print("""/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "config.h"
+
+#include "qtype.h"
+
+const perf_qtype_t qtype_table[] = {""")
+
+for k, v in qtype.items():
+ if k == "Unassigned" or k == "Reserved":
+ continue
+ if k == "*":
+ k = "ANY"
+ print(" { \"%s\", %d }," % (k, v))
+
+print(""" { 0, 0 }
+};""")
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef PERF_LIST_H
+#define PERF_LIST_H 1
+
+#include <assert.h>
+
+#define perf_link(type) \
+ struct { \
+ type *prev, *next; \
+ } _link
+#define perf_link_init(link) \
+ { \
+ (link)->_link.prev = 0; \
+ (link)->_link.next = 0; \
+ }
+
+#define perf_list(type) \
+ struct { \
+ type *head, *tail; \
+ }
+#define perf_list_init(list) \
+ { \
+ (list).head = 0; \
+ (list).tail = 0; \
+ }
+
+#define perf_list_head(list) ((list).head)
+#define perf_list_tail(list) ((list).tail)
+#define perf_list_empty(list) (!(list).head)
+
+#define perf_list_append(list, link) \
+ { \
+ if ((list).tail) { \
+ (list).tail->_link.next = (link); \
+ } else { \
+ (list).head = (link); \
+ } \
+ (link)->_link.prev = (list).tail; \
+ (link)->_link.next = 0; \
+ (list).tail = (link); \
+ }
+#define perf_list_prepend(list, link) \
+ { \
+ if ((list).head) { \
+ (list).head->_link.prev = (link); \
+ } else { \
+ (list).tail = (link); \
+ } \
+ (link)->_link.prev = 0; \
+ (link)->_link.next = (list).head; \
+ (list).head = (link); \
+ }
+#define perf_list_unlink(list, link) \
+ { \
+ if ((link)->_link.next) { \
+ (link)->_link.next->_link.prev = (link)->_link.prev; \
+ } else { \
+ assert((list).tail == (link)); \
+ (list).tail = (link)->_link.prev; \
+ } \
+ if ((link)->_link.prev) { \
+ (link)->_link.prev->_link.next = (link)->_link.next; \
+ } else { \
+ assert((list).head == (link)); \
+ (list).head = (link)->_link.next; \
+ } \
+ (link)->_link.next = 0; \
+ (link)->_link.prev = 0; \
+ assert((list).head != (link)); \
+ assert((list).tail != (link)); \
+ }
+
+#endif
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "config.h"
+
+#include "log.h"
+
+#include "util.h"
+
+#include <pthread.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+static bool log_err_stdout = false;
+
+pthread_mutex_t log_lock = PTHREAD_MUTEX_INITIALIZER;
+
+static void
+vlog(FILE* stream, const char* prefix, const char* fmt, va_list args)
+{
+ PERF_LOCK(&log_lock);
+ fflush(stdout);
+ if (prefix != NULL)
+ fprintf(stream, "%s: ", prefix);
+ vfprintf(stream, fmt, args);
+ fprintf(stream, "\n");
+ PERF_UNLOCK(&log_lock);
+}
+
+void perf_log_printf(const char* fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ vlog(stdout, NULL, fmt, args);
+}
+
+void perf_log_fatal(const char* fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ vlog(log_err_stdout ? stdout : stderr, "Error", fmt, args);
+ exit(1);
+}
+
+void perf_log_warning(const char* fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ vlog(log_err_stdout ? stdout : stderr, "Warning", fmt, args);
+}
+
+void perf_log_tostdout(void)
+{
+ log_err_stdout = true;
+}
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef PERF_LOG_H
+#define PERF_LOG_H 1
+
+void perf_log_printf(const char* fmt, ...);
+void perf_log_fatal(const char* fmt, ...);
+void perf_log_warning(const char* fmt, ...);
+void perf_log_tostdout(void);
+
+#endif
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "config.h"
+
+#include "net.h"
+
+#include "log.h"
+#include "opt.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <string.h>
+#include <poll.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+
+const char* perf_net_tls_sni = 0;
+
+enum perf_net_mode perf_net_parsemode(const char* mode)
+{
+ if (!strcmp(mode, "udp")) {
+ return sock_udp;
+ } else if (!strcmp(mode, "tcp")) {
+ return sock_tcp;
+ } else if (!strcmp(mode, "tls") || !strcmp(mode, "dot")) {
+ return sock_dot;
+ } else if (!strcmp(mode, "doh")) {
+ return sock_doh;
+ }
+
+ perf_log_warning("invalid socket mode");
+ perf_opt_usage();
+ exit(1);
+}
+
+int perf_net_parsefamily(const char* family)
+{
+ if (family == NULL || strcmp(family, "any") == 0)
+ return AF_UNSPEC;
+ else if (strcmp(family, "inet") == 0)
+ return AF_INET;
+ else if (strcmp(family, "inet6") == 0)
+ return AF_INET6;
+ else {
+ fprintf(stderr, "invalid family %s\n", family);
+ perf_opt_usage();
+ exit(1);
+ }
+}
+
+void perf_sockaddr_fromin(perf_sockaddr_t* sockaddr, const struct in_addr* in, in_port_t port)
+{
+ memset(sockaddr, 0, sizeof(*sockaddr));
+ sockaddr->sa.sin.sin_family = AF_INET;
+ sockaddr->sa.sin.sin_addr = *in;
+ sockaddr->sa.sin.sin_port = htons(port);
+ sockaddr->length = sizeof(sockaddr->sa.sin);
+}
+
+void perf_sockaddr_fromin6(perf_sockaddr_t* sockaddr, const struct in6_addr* in, in_port_t port)
+{
+ memset(sockaddr, 0, sizeof(*sockaddr));
+ sockaddr->sa.sin6.sin6_family = AF_INET6;
+ sockaddr->sa.sin6.sin6_addr = *in;
+ sockaddr->sa.sin6.sin6_port = htons(port);
+ sockaddr->length = sizeof(sockaddr->sa.sin6);
+}
+
+in_port_t perf_sockaddr_port(const perf_sockaddr_t* sockaddr)
+{
+ switch (sockaddr->sa.sa.sa_family) {
+ case AF_INET:
+ return ntohs(sockaddr->sa.sin.sin_port);
+ case AF_INET6:
+ return ntohs(sockaddr->sa.sin6.sin6_port);
+ default:
+ break;
+ }
+ return 0;
+}
+
+void perf_sockaddr_setport(perf_sockaddr_t* sockaddr, in_port_t port)
+{
+ switch (sockaddr->sa.sa.sa_family) {
+ case AF_INET:
+ sockaddr->sa.sin.sin_port = htons(port);
+ break;
+ case AF_INET6:
+ sockaddr->sa.sin6.sin6_port = htons(port);
+ break;
+ default:
+ break;
+ }
+}
+
+void perf_sockaddr_format(const perf_sockaddr_t* sockaddr, char* buf, size_t len)
+{
+ const void* src;
+
+ *buf = 0;
+
+ switch (sockaddr->sa.sa.sa_family) {
+ case AF_INET:
+ src = &sockaddr->sa.sin.sin_addr;
+ break;
+ case AF_INET6:
+ src = &sockaddr->sa.sin6.sin6_addr;
+ break;
+ default:
+ return;
+ }
+
+ (void)inet_ntop(sockaddr->sa.sa.sa_family, src, buf, len);
+}
+
+void perf_net_parseserver(int family, const char* name, unsigned int port, perf_sockaddr_t* addr)
+{
+ struct addrinfo* ai;
+
+ if (getaddrinfo(name, 0, 0, &ai) == 0) {
+ struct addrinfo* a;
+
+ for (a = ai; a; a = a->ai_next) {
+ if (a->ai_family == family || family == AF_UNSPEC) {
+ switch (a->ai_family) {
+ case AF_INET:
+ perf_sockaddr_fromin(addr, &((struct sockaddr_in*)a->ai_addr)->sin_addr, port);
+ break;
+ case AF_INET6:
+ perf_sockaddr_fromin6(addr, &((struct sockaddr_in6*)a->ai_addr)->sin6_addr, port);
+ break;
+ default:
+ continue;
+ }
+
+ freeaddrinfo(ai);
+ return;
+ }
+ }
+ freeaddrinfo(ai);
+ }
+
+ fprintf(stderr, "invalid server address %s\n", name);
+ perf_opt_usage();
+ exit(1);
+}
+
+void perf_net_parselocal(int family, const char* name, unsigned int port,
+ perf_sockaddr_t* addr)
+{
+ struct in_addr in4a;
+ struct in6_addr in6a;
+
+ if (name == NULL) {
+ switch (family) {
+ case AF_INET:
+ in4a.s_addr = INADDR_ANY;
+ perf_sockaddr_fromin(addr, &in4a, port);
+ return;
+ case AF_INET6:
+ perf_sockaddr_fromin6(addr, &in6addr_any, port);
+ return;
+ default:
+ break;
+ }
+ } else if (inet_pton(AF_INET, name, &in4a) == 1) {
+ perf_sockaddr_fromin(addr, &in4a, port);
+ return;
+ } else if (inet_pton(AF_INET6, name, &in6a) == 1) {
+ perf_sockaddr_fromin6(addr, &in6a, port);
+ return;
+ }
+
+ fprintf(stderr, "invalid local address %s\n", name);
+ perf_opt_usage();
+ exit(1);
+}
+
+struct perf_net_socket* perf_net_opensocket(enum perf_net_mode mode, const perf_sockaddr_t* server, const perf_sockaddr_t* local, unsigned int offset, size_t bufsize, void* data, perf_net_sent_cb_t sent, perf_net_event_cb_t event)
+{
+ int port;
+ perf_sockaddr_t tmp;
+
+ if (server->sa.sa.sa_family != local->sa.sa.sa_family) {
+ perf_log_fatal("server and local addresses have different families");
+ }
+
+ tmp = *local;
+ port = perf_sockaddr_port(&tmp);
+ if (port != 0 && offset != 0) {
+ port += offset;
+ if (port >= 0xFFFF)
+ perf_log_fatal("port %d out of range", port);
+ perf_sockaddr_setport(&tmp, port);
+ }
+
+ switch (mode) {
+ case sock_udp:
+ return perf_net_udp_opensocket(server, &tmp, bufsize, data, sent, event);
+ case sock_tcp:
+ return perf_net_tcp_opensocket(server, &tmp, bufsize, data, sent, event);
+ case sock_dot:
+ return perf_net_dot_opensocket(server, &tmp, bufsize, data, sent, event);
+ case sock_doh:
+ return perf_net_doh_opensocket(server, &tmp, bufsize, data, sent, event);
+ default:
+ perf_log_fatal("perf_net_opensocket(): invalid mode");
+ }
+
+ return 0;
+}
+
+void perf_net_stats_init(enum perf_net_mode mode)
+{
+ switch (mode) {
+ case sock_doh:
+ perf_net_doh_stats_init();
+ default:
+ break;
+ }
+}
+
+void perf_net_stats_compile(enum perf_net_mode mode, struct perf_net_socket* sock)
+{
+ switch (mode) {
+ case sock_doh:
+ perf_net_doh_stats_compile(sock);
+ default:
+ break;
+ }
+}
+
+void perf_net_stats_print(enum perf_net_mode mode, bool json)
+{
+ switch (mode) {
+ case sock_doh:
+ perf_net_doh_stats_print(json);
+ default:
+ break;
+ }
+}
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef PERF_NET_H
+#define PERF_NET_H 1
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <openssl/ssl.h>
+#include <pthread.h>
+#include <netinet/in.h>
+#include <assert.h>
+#include <stdbool.h>
+
+#define TCP_RECV_BUF_SIZE (65535 + 2)
+#define TCP_SEND_BUF_SIZE (65535 + 2)
+
+struct perf_sockaddr {
+ union {
+ struct sockaddr sa;
+ struct sockaddr_in sin;
+ struct sockaddr_in6 sin6;
+ } sa;
+ socklen_t length;
+};
+typedef struct perf_sockaddr perf_sockaddr_t;
+
+enum perf_net_mode {
+ sock_none,
+ sock_file,
+ sock_pipe,
+ sock_udp,
+ sock_tcp,
+ sock_dot,
+ sock_doh
+};
+
+struct perf_net_socket;
+
+typedef ssize_t (*perf_net_recv_t)(struct perf_net_socket* sock, void* buf, size_t len, int flags);
+typedef ssize_t (*perf_net_sendto_t)(struct perf_net_socket* sock, uint16_t qid, const void* buf, size_t len, int flags, const struct sockaddr* dest_addr, socklen_t addrlen);
+typedef int (*perf_net_close_t)(struct perf_net_socket* sock);
+typedef int (*perf_net_sockeq_t)(struct perf_net_socket* sock, struct perf_net_socket* other);
+
+/* sockready return:
+ * -1: socket readiness timed out / canceled / interrupted or unknown error
+ * 0: Socket is not ready, may still be connecting, negotiating or sending
+ * 1: Socket is ready and can be used for sending to
+ */
+typedef int (*perf_net_sockready_t)(struct perf_net_socket* sock, int pipe_fd, int64_t timeout);
+
+/* Indicates if there are more data to be read in buffers of the transport */
+typedef bool (*perf_net_have_more_t)(struct perf_net_socket* sock);
+
+/* Callback for when a query has been sent if it was delayed due to partily sent or reconnection */
+typedef void (*perf_net_sent_cb_t)(struct perf_net_socket* sock, uint16_t qid);
+
+typedef enum perf_socket_event {
+ perf_socket_event_connecting,
+ perf_socket_event_connected,
+ perf_socket_event_reconnecting,
+ perf_socket_event_reconnected
+} perf_socket_event_t;
+/* Callback for socket events related to connection oriented protocols, for statistics */
+typedef void (*perf_net_event_cb_t)(struct perf_net_socket* sock, perf_socket_event_t event, uint64_t elapsed_time);
+
+typedef void (*perf_net_num_queries_per_conn_t)(struct perf_net_socket* sock, size_t num_queries_per_conn, size_t timeout);
+
+struct perf_net_socket {
+ void* data; /* user data */
+
+ enum perf_net_mode mode;
+ perf_net_recv_t recv;
+ perf_net_sendto_t sendto;
+ perf_net_close_t close;
+ perf_net_sockeq_t sockeq;
+ perf_net_sockready_t sockready;
+ perf_net_have_more_t have_more;
+
+ perf_net_num_queries_per_conn_t num_queries_per_conn;
+
+ /*
+ * Not set by protocol, set by caller.
+ * May be 0 if caller don't care.
+ * MUST NOT be called from sendto(), only called if query is delayed in some way.
+ */
+ perf_net_sent_cb_t sent;
+
+ /* Used if caller want info on connection oriented events */
+ perf_net_event_cb_t event;
+
+ /*
+ * The system file descriptor that is used for transport, this is used
+ * in os functions to poll/wait for read/write.
+ */
+ int fd;
+};
+
+static inline ssize_t perf_net_recv(struct perf_net_socket* sock, void* buf, size_t len, int flags)
+{
+ return sock->recv(sock, buf, len, flags);
+}
+
+static inline ssize_t perf_net_sendto(struct perf_net_socket* sock, uint16_t qid, const void* buf, size_t len, int flags, const struct sockaddr* dest_addr, socklen_t addrlen)
+{
+ return sock->sendto(sock, qid, buf, len, flags, dest_addr, addrlen);
+}
+
+static inline int perf_net_close(struct perf_net_socket* sock)
+{
+ return sock->close(sock);
+}
+
+static inline int perf_net_sockeq(struct perf_net_socket* sock_a, struct perf_net_socket* sock_b)
+{
+ assert(sock_a);
+ assert(sock_b);
+ assert(sock_a->mode == sock_b->mode);
+ return sock_a->sockeq(sock_a, sock_b);
+}
+
+static inline int perf_net_sockready(struct perf_net_socket* sock, int pipe_fd, int64_t timeout)
+{
+ return sock->sockready(sock, pipe_fd, timeout);
+}
+
+static inline int perf_net_have_more(struct perf_net_socket* sock)
+{
+ return sock->have_more ? sock->have_more(sock) : false;
+}
+
+enum perf_net_mode perf_net_parsemode(const char* mode);
+
+int perf_net_parsefamily(const char* family);
+
+void perf_net_parseserver(int family, const char* name, unsigned int port, perf_sockaddr_t* addr);
+void perf_net_parselocal(int family, const char* name, unsigned int port, perf_sockaddr_t* addr);
+
+void perf_sockaddr_fromin(perf_sockaddr_t* sockaddr, const struct in_addr* in, in_port_t port);
+void perf_sockaddr_fromin6(perf_sockaddr_t* sockaddr, const struct in6_addr* in, in_port_t port);
+in_port_t perf_sockaddr_port(const perf_sockaddr_t* sockaddr);
+void perf_sockaddr_setport(perf_sockaddr_t* sockaddr, in_port_t port);
+void perf_sockaddr_format(const perf_sockaddr_t* sockaddr, char* buf, size_t len);
+
+static inline int perf_sockaddr_isinet6(const perf_sockaddr_t* sockaddr)
+{
+ return sockaddr->sa.sa.sa_family == AF_INET6;
+}
+
+struct perf_net_socket* perf_net_opensocket(enum perf_net_mode mode, const perf_sockaddr_t* server, const perf_sockaddr_t* local, unsigned int offset, size_t bufsize, void* data, perf_net_sent_cb_t sent, perf_net_event_cb_t event);
+
+struct perf_net_socket* perf_net_udp_opensocket(const perf_sockaddr_t*, const perf_sockaddr_t*, size_t, void* data, perf_net_sent_cb_t sent, perf_net_event_cb_t event);
+struct perf_net_socket* perf_net_tcp_opensocket(const perf_sockaddr_t*, const perf_sockaddr_t*, size_t, void* data, perf_net_sent_cb_t sent, perf_net_event_cb_t event);
+struct perf_net_socket* perf_net_dot_opensocket(const perf_sockaddr_t*, const perf_sockaddr_t*, size_t, void* data, perf_net_sent_cb_t sent, perf_net_event_cb_t event);
+struct perf_net_socket* perf_net_doh_opensocket(const perf_sockaddr_t*, const perf_sockaddr_t*, size_t, void* data, perf_net_sent_cb_t sent, perf_net_event_cb_t event);
+
+#define DEFAULT_DOH_URI "https://localhost/dns-query"
+#define DEFAULT_DOH_METHOD "GET"
+
+void perf_net_doh_parse_uri(const char*);
+void perf_net_doh_parse_method(const char*);
+void perf_net_doh_set_max_concurrent_streams(size_t);
+
+void perf_net_stats_init(enum perf_net_mode);
+void perf_net_stats_compile(enum perf_net_mode, struct perf_net_socket*);
+void perf_net_stats_print(enum perf_net_mode, bool json);
+void perf_net_doh_stats_init();
+void perf_net_doh_stats_compile(struct perf_net_socket*);
+void perf_net_doh_stats_print(bool json);
+
+extern const char* perf_net_tls_sni;
+
+#endif
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/*
+ * Based on HTTP/2 module in DNS shotgun by Tomáš Křížek (CZ.NIC)
+ * https://gitlab.nic.cz/knot/shotgun/-/blob/master/replay/dnssim/src/output/dnssim/https2.c
+ *
+ * Initial PoC implementation by Atanas Argirov (PeeriX)
+ * https://github.com/m0rcq
+ */
+
+#include "config.h"
+
+#include "net.h"
+#include "edns.h"
+#include "ext/parse_uri.h"
+#include "log.h"
+#include "strerror.h"
+#include "util.h"
+#include "os.h"
+#include "opt.h"
+
+#include <errno.h>
+#include <assert.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <openssl/err.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <ck_pr.h>
+#include <nghttp2/nghttp2.h>
+#include <openssl/bio.h>
+#include <openssl/evp.h>
+#ifdef HAVE_JSON_C
+#include <json-c/json.h>
+#endif
+
+#define DNS_GET_REQUEST_VAR "?dns="
+#define DNS_MSG_MAX_SIZE 65535
+
+static SSL_CTX* ssl_ctx = 0;
+static struct URI doh_uri;
+enum perf_doh_method {
+ doh_method_get,
+ doh_method_post
+};
+static enum perf_doh_method doh_method = doh_method_get;
+static size_t doh_max_concurr = 100;
+
+#define self ((struct perf__doh_socket*)sock)
+
+#define MAKE_NV(NAME, VALUE) \
+ { \
+ (uint8_t*)NAME, (uint8_t*)VALUE, sizeof(NAME) - 1, sizeof(VALUE) - 1, \
+ NGHTTP2_NV_FLAG_NONE \
+ }
+
+#define MAKE_NV_LEN(NAME, VALUE, VALUELEN) \
+ { \
+ (uint8_t*)NAME, (uint8_t*)VALUE, sizeof(NAME) - 1, VALUELEN, \
+ NGHTTP2_NV_FLAG_NONE \
+ }
+
+typedef struct {
+ uint8_t *buf, *bufp;
+ size_t len, buf_len;
+} http2_data_provider_t;
+
+typedef struct {
+ nghttp2_session* session;
+ http2_data_provider_t payload;
+ bool settings_sent;
+ char dnsmsg[DNS_MSG_MAX_SIZE];
+ size_t dnsmsg_at;
+ bool dnsmsg_completed;
+} http2_session_t;
+
+struct _doh_stats {
+ size_t code[500];
+ size_t unknown_code;
+};
+
+struct perf__doh_socket {
+ struct perf_net_socket base;
+
+ pthread_mutex_t lock;
+ SSL* ssl;
+
+ bool is_ready, is_conn_ready, is_ssl_ready, is_sending, is_post_sending;
+ // bool have_more; TODO
+ bool do_reconnect, do_connect;
+
+ perf_sockaddr_t server, local;
+ size_t bufsize;
+
+ uint16_t qid;
+
+ uint64_t conn_ts;
+ perf_socket_event_t conn_event, conning_event;
+
+ http2_session_t http2; // http2 session data
+ int http2_code;
+ bool http2_is_dns;
+
+ struct _doh_stats stats;
+
+ char recvbuf[TCP_RECV_BUF_SIZE];
+ size_t recv_at;
+
+ size_t num_queries_per_conn, nqpc_timeout;
+ unsigned int nqpc_sent, nqpc_recv;
+ uint64_t nqpc_ts;
+};
+
+static pthread_mutex_t _nghttp2_lock = PTHREAD_MUTEX_INITIALIZER;
+static nghttp2_session_callbacks* _nghttp2_callbacks = 0;
+static nghttp2_option* _nghttp2_option = 0;
+
+void perf_net_doh_parse_uri(const char* uri)
+{
+ if (parse_uri(&doh_uri, uri)) {
+ perf_log_warning("invalid DNS-over-HTTPS URI");
+ perf_opt_usage();
+ exit(1);
+ }
+}
+
+void perf_net_doh_parse_method(const char* method)
+{
+ if (!strcmp(method, "GET")) {
+ doh_method = doh_method_get;
+ return;
+ } else if (!strcmp(method, "POST")) {
+ doh_method = doh_method_post;
+ return;
+ }
+
+ perf_log_warning("invalid DNS-over-HTTPS method");
+ perf_opt_usage();
+ exit(1);
+}
+
+void perf_net_doh_set_max_concurrent_streams(size_t max_concurr)
+{
+ doh_max_concurr = max_concurr;
+}
+
+static void perf__doh_connect(struct perf_net_socket* sock)
+{
+ int ret;
+
+ self->nqpc_sent = 0;
+ ck_pr_store_uint(&self->nqpc_recv, 0);
+ self->nqpc_ts = 0;
+
+ nghttp2_session_del(self->http2.session);
+ ret = nghttp2_session_client_new2(&self->http2.session, _nghttp2_callbacks, sock, _nghttp2_option);
+ if (ret < 0) {
+ perf_log_fatal("Failed to initialize http2 session: %s", nghttp2_strerror(ret));
+ }
+
+ int fd = socket(self->server.sa.sa.sa_family, SOCK_STREAM, 0);
+ if (fd == -1) {
+ char __s[256];
+ perf_log_fatal("socket: %s", perf_strerror_r(errno, __s, sizeof(__s)));
+ }
+ ck_pr_store_int(&sock->fd, fd);
+
+ if (self->ssl) {
+ SSL_free(self->ssl);
+ }
+ if (!(self->ssl = SSL_new(ssl_ctx))) {
+ perf_log_fatal("SSL_new(): %s", ERR_error_string(ERR_get_error(), 0));
+ }
+ if (perf_net_tls_sni && !(ret = SSL_set_tlsext_host_name(self->ssl, perf_net_tls_sni))) {
+ perf_log_fatal("SSL_set_tlsext_host_name(): %s", ERR_error_string(SSL_get_error(self->ssl, ret), 0));
+ }
+ if (!(ret = SSL_set_fd(self->ssl, sock->fd))) {
+ perf_log_fatal("SSL_set_fd(): %s", ERR_error_string(SSL_get_error(self->ssl, ret), 0));
+ }
+
+ if (self->server.sa.sa.sa_family == AF_INET6) {
+ int on = 1;
+
+ if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) == -1) {
+ perf_log_warning("setsockopt(IPV6_V6ONLY) failed");
+ }
+ }
+
+ if (bind(sock->fd, &self->local.sa.sa, self->local.length) == -1) {
+ char __s[256];
+ perf_log_fatal("bind: %s", perf_strerror_r(errno, __s, sizeof(__s)));
+ }
+
+ if (self->bufsize > 0) {
+ ret = setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
+ &self->bufsize, sizeof(self->bufsize));
+ if (ret < 0)
+ perf_log_warning("setsockbuf(SO_RCVBUF) failed");
+
+ ret = setsockopt(sock->fd, SOL_SOCKET, SO_SNDBUF,
+ &self->bufsize, sizeof(self->bufsize));
+ if (ret < 0)
+ perf_log_warning("setsockbuf(SO_SNDBUF) failed");
+ }
+
+ int flags = fcntl(sock->fd, F_GETFL, 0);
+ if (flags < 0)
+ perf_log_fatal("fcntl(F_GETFL)");
+ ret = fcntl(sock->fd, F_SETFL, flags | O_NONBLOCK);
+ if (ret < 0)
+ perf_log_fatal("fcntl(F_SETFL)");
+
+ self->conn_ts = perf_get_time();
+ if (sock->event) {
+ sock->event(sock, self->conning_event, self->conn_ts);
+ self->conning_event = perf_socket_event_reconnecting;
+ }
+ if (connect(sock->fd, &self->server.sa.sa, self->server.length)) {
+ if (errno == EINPROGRESS) {
+ return;
+ } else {
+ char __s[256];
+ perf_log_fatal("connect() failed: %s", perf_strerror_r(errno, __s, sizeof(__s)));
+ }
+ }
+
+ self->is_conn_ready = true;
+}
+
+static void perf__doh_reconnect(struct perf_net_socket* sock)
+{
+ close(sock->fd);
+ // self->have_more = false; TODO
+
+ self->http2.settings_sent = false;
+ self->is_ready = false;
+ self->is_conn_ready = false;
+ self->is_ssl_ready = false;
+ self->is_sending = false;
+ self->is_post_sending = false;
+
+ self->http2.dnsmsg_at = 0;
+ self->http2.dnsmsg_completed = false;
+ self->http2_code = 0;
+ self->http2_is_dns = false;
+
+ self->do_connect = true;
+}
+
+// static ssize_t _recv_callback(nghttp2_session *session, uint8_t *buf, size_t length, int flags, void *sock)
+// {
+// if (self->http2.dnsmsg_completed) {
+// return NGHTTP2_ERR_WOULDBLOCK;
+// }
+//
+// ssize_t n = SSL_read(self->ssl, buf, length);
+// if (!n) {
+// perf__doh_reconnect(sock);
+// return NGHTTP2_ERR_WOULDBLOCK;
+// }
+// if (n < 0) {
+// int err = SSL_get_error(self->ssl, n);
+// switch (err) {
+// case SSL_ERROR_WANT_READ:
+// return NGHTTP2_ERR_WOULDBLOCK;
+// case SSL_ERROR_SYSCALL:
+// switch (errno) {
+// case ECONNREFUSED:
+// case ECONNRESET:
+// case ENOTCONN:
+// perf__doh_reconnect(sock);
+// return NGHTTP2_ERR_WOULDBLOCK;
+// default:
+// break;
+// }
+// break;
+// default:
+// break;
+// }
+// return NGHTTP2_ERR_CALLBACK_FAILURE;
+// }
+// return n;
+// }
+
+static ssize_t perf__doh_recv(struct perf_net_socket* sock, void* buf, size_t len, int flags)
+{
+ // read TLS data here instead of nghttp2_recv_callback
+ PERF_LOCK(&self->lock);
+ if (!self->is_ready) {
+ PERF_UNLOCK(&self->lock);
+ errno = EAGAIN;
+ return -1;
+ }
+
+ if (self->recv_at < sizeof(self->recvbuf)) {
+ // try to recv more if we can
+ ssize_t n = SSL_read(self->ssl, self->recvbuf + self->recv_at, sizeof(self->recvbuf) - self->recv_at);
+ if (!n) {
+ perf__doh_reconnect(sock);
+ PERF_UNLOCK(&self->lock);
+ errno = EAGAIN;
+ return -1;
+ }
+ if (n < 0) {
+ int err = SSL_get_error(self->ssl, n);
+ switch (err) {
+ case SSL_ERROR_WANT_READ:
+ if (self->recv_at) {
+ // did not recv but we got something in buffer
+ break;
+ }
+ errno = EAGAIN;
+ PERF_UNLOCK(&self->lock);
+ return -1;
+ case SSL_ERROR_SYSCALL:
+ switch (errno) {
+ case EBADF:
+ // treat this as a retry, can happen if sendto is reconnecting
+ case ECONNREFUSED:
+ case ECONNRESET:
+ case ENOTCONN:
+ perf__doh_reconnect(sock);
+ errno = EAGAIN;
+ break;
+ default:
+ break;
+ }
+ PERF_UNLOCK(&self->lock);
+ return -1;
+ default:
+ errno = EBADF;
+ PERF_UNLOCK(&self->lock);
+ return -1;
+ }
+ } else {
+ self->recv_at += n;
+ }
+ }
+
+ // this will be processed by nghttp2 callbacks
+ ssize_t ret = nghttp2_session_mem_recv(self->http2.session, (uint8_t*)self->recvbuf, self->recv_at);
+ if (ret < 0) {
+ perf_log_warning("nghttp2_session_mem_recv failed: %s", nghttp2_strerror(ret));
+ PERF_UNLOCK(&self->lock);
+ return -1;
+ }
+ if (ret != self->recv_at) {
+ // only process one response per call
+ memmove(self->recvbuf, self->recvbuf + ret, self->recv_at - ret);
+ self->recv_at -= ret;
+ } else {
+ self->recv_at = 0;
+ }
+
+ // TODO: is this faster then mem_recv?
+ // int ret = nghttp2_session_recv(self->http2.session);
+ // if (ret < 0) {
+ // perf_log_warning("nghttp2_session_recv failed: %s", nghttp2_strerror(ret));
+ // PERF_UNLOCK(&self->lock);
+ // return -1;
+ // }
+
+ if (self->http2.dnsmsg_completed) {
+ if (self->http2_code > 99 && self->http2_code < 600) {
+ self->stats.code[self->http2_code - 100]++;
+ } else {
+ self->stats.unknown_code++;
+ }
+ if (!self->http2_is_dns) {
+ // TODO: store non-dns for stats
+ self->http2.dnsmsg_completed = false;
+ self->http2.dnsmsg_at = 0;
+ self->http2_code = 0;
+ self->http2_is_dns = false;
+ PERF_UNLOCK(&self->lock);
+ errno = EAGAIN;
+ return -1;
+ }
+ if (self->http2_code < 200 || self->http2_code > 299) {
+ // TODO: store return code for stats
+ self->http2.dnsmsg_completed = false;
+ self->http2.dnsmsg_at = 0;
+ self->http2_code = 0;
+ PERF_UNLOCK(&self->lock);
+ errno = EAGAIN;
+ return -1;
+ }
+ if (self->http2.dnsmsg_at < len) {
+ len = self->http2.dnsmsg_at;
+ }
+ memcpy(buf, self->http2.dnsmsg, len);
+ self->http2.dnsmsg_completed = false;
+ self->http2.dnsmsg_at = 0;
+
+ // self->have_more = false; TODO
+ PERF_UNLOCK(&self->lock);
+ if (self->num_queries_per_conn) {
+ ck_pr_inc_uint(&self->nqpc_recv);
+ }
+ return len;
+ }
+
+ // self->have_more = true; TODO
+ PERF_UNLOCK(&self->lock);
+ errno = EAGAIN;
+ return -1;
+}
+
+static void _submit_dns_query_get(struct perf_net_socket* sock, const void* buf, size_t len)
+{
+ const size_t path_len = doh_uri.pathlen
+ + sizeof(DNS_GET_REQUEST_VAR) - 1
+ + (4 * ((len + 2) / 3)) + 1;
+ char full_path[path_len];
+ char* p = &full_path[0];
+
+ memcpy(p, doh_uri.path, doh_uri.pathlen);
+ p += doh_uri.pathlen;
+
+ memcpy(p, DNS_GET_REQUEST_VAR, sizeof(DNS_GET_REQUEST_VAR) - 1);
+ p += sizeof(DNS_GET_REQUEST_VAR) - 1;
+
+ EVP_EncodeBlock((unsigned char*)p, buf, len);
+ // RFC8484 requires base64url (RFC4648)
+ // and Padding characters (=) for base64url MUST NOT be included.
+ // base64url alphabet is the same as base64 except + is - and / is _
+ while (*p) {
+ switch (*p) {
+ case '+':
+ *p++ = '-';
+ break;
+ case '/':
+ *p++ = '_';
+ break;
+ case '=':
+ *p = 0;
+ break;
+ default:
+ p++;
+ }
+ }
+
+ const nghttp2_nv hdrs[] = {
+ MAKE_NV(":method", "GET"),
+ MAKE_NV(":scheme", "https"),
+ MAKE_NV_LEN(":authority", doh_uri.hostport, doh_uri.hostportlen),
+ MAKE_NV_LEN(":path", full_path, p - full_path),
+ MAKE_NV("accept", "application/dns-message"),
+ MAKE_NV("user-agent", "dnsperf/" PACKAGE_VERSION " (nghttp2/" NGHTTP2_VERSION ")")
+ };
+
+ int32_t stream_id = nghttp2_submit_request(self->http2.session,
+ NULL,
+ hdrs,
+ sizeof(hdrs) / sizeof(hdrs[0]),
+ NULL,
+ sock);
+ if (stream_id < 0) {
+ perf_log_fatal("Failed to submit HTTP2 request: %s", nghttp2_strerror(stream_id));
+ }
+}
+
+static ssize_t _payload_read_cb(nghttp2_session* session,
+ int32_t stream_id, uint8_t* buf,
+ size_t length, uint32_t* data_flags,
+ nghttp2_data_source* source,
+ void* sock)
+{
+ http2_data_provider_t* payload = source->ptr;
+
+ ssize_t payload_size = length < payload->len ? length : payload->len;
+
+ memcpy(buf, payload->bufp, payload_size);
+ payload->bufp += payload_size;
+ payload->len -= payload_size;
+ // check for EOF
+ if (payload->len == 0) {
+ *data_flags |= NGHTTP2_DATA_FLAG_EOF;
+ self->is_post_sending = false;
+ }
+
+ return payload_size;
+}
+
+static void _submit_dns_query_post(struct perf_net_socket* sock, const void* buf, size_t len)
+{
+ // POST requires DATA flow-controlled payload that local endpoint
+ // can send across without issuing WINDOW_UPDATE
+ // we need to check for this and bounce back the request if the
+ // payload > remote window size
+ // TODO: are below needed? can they be checked on connect?
+ // int remote_window_size = nghttp2_session_get_remote_window_size(self->http2.session);
+ // if (remote_window_size < 0) {
+ // perf_log_fatal("failed to get http2 session remote window size");
+ // }
+ // if (len > remote_window_size) {
+ // perf_log_fatal("remote window size is too small for POST payload");
+ // }
+
+ // compose content-length
+ char payload_size[20];
+ int payload_size_len = snprintf(payload_size, sizeof(payload_size), "%zu", len);
+ // TODO: check snprintf()
+
+ const nghttp2_nv hdrs[] = {
+ MAKE_NV(":method", "POST"),
+ MAKE_NV(":scheme", "https"),
+ MAKE_NV_LEN(":authority", doh_uri.hostport, doh_uri.hostportlen),
+ MAKE_NV_LEN(":path", doh_uri.path, doh_uri.pathlen),
+ MAKE_NV("accept", "application/dns-message"),
+ MAKE_NV("content-type", "application/dns-message"),
+ MAKE_NV_LEN("content-length", payload_size, payload_size_len),
+ MAKE_NV("user-agent", "dnsperf/" PACKAGE_VERSION " (nghttp2/" NGHTTP2_VERSION ")")
+ };
+
+ if (len > self->http2.payload.buf_len) {
+ self->http2.payload.buf_len = ((len / MAX_EDNS_PACKET) + 1) * MAX_EDNS_PACKET;
+ if (!(self->http2.payload.buf = realloc(self->http2.payload.buf, self->http2.payload.buf_len))) {
+ perf_log_fatal("perf_net_doh: out of memory");
+ }
+ }
+ if (self && self->http2.payload.buf && buf) { // fix clang scan-build
+ memcpy(self->http2.payload.buf, buf, len);
+ } else {
+ perf_log_fatal("_submit_dns_query_post(): payload.buf is null");
+ }
+ self->http2.payload.bufp = self->http2.payload.buf;
+ self->http2.payload.len = len;
+ self->is_post_sending = true;
+
+ // we need data provider to pass to submit()
+
+ nghttp2_data_provider data_provider = {
+ .source.ptr = &self->http2.payload,
+ .read_callback = _payload_read_cb
+ };
+ int32_t stream_id = nghttp2_submit_request(self->http2.session,
+ NULL,
+ hdrs,
+ sizeof(hdrs) / sizeof(hdrs[0]),
+ &data_provider,
+ sock);
+ if (stream_id < 0) {
+ perf_log_fatal("Failed to submit HTTP2 request: %s", nghttp2_strerror(stream_id));
+ }
+}
+
+static ssize_t perf__doh_sendto(struct perf_net_socket* sock, uint16_t qid, const void* buf, size_t len, int flags, const struct sockaddr* dest_addr, socklen_t addrlen)
+{
+ PERF_LOCK(&self->lock);
+
+ if (!self->is_ready) {
+ // TODO: query will be lost here
+ PERF_UNLOCK(&self->lock);
+ errno = EINPROGRESS;
+ return -1;
+ }
+
+ if (self->is_sending) {
+ perf_log_fatal("called when sending");
+ }
+
+ self->qid = qid;
+
+ switch (doh_method) {
+ case doh_method_get:
+ _submit_dns_query_get(sock, buf, len);
+ break;
+ case doh_method_post:
+ _submit_dns_query_post(sock, buf, len);
+ break;
+ }
+
+ int ret = nghttp2_session_send(self->http2.session);
+ if (ret < 0) {
+ // TODO: handle error better, reconnect when needed
+ perf_log_warning("nghttp2_session_send failed: %s", nghttp2_strerror(ret));
+ self->do_reconnect = true;
+ PERF_UNLOCK(&self->lock);
+ errno = EINPROGRESS;
+ return -1;
+ }
+
+ if (self->is_post_sending || nghttp2_session_get_outbound_queue_size(self->http2.session) > 0 || nghttp2_session_want_write(self->http2.session)) {
+ self->is_sending = true;
+ PERF_UNLOCK(&self->lock);
+ errno = EINPROGRESS;
+ return -1;
+ }
+ PERF_UNLOCK(&self->lock);
+
+ self->nqpc_sent++;
+
+ return len;
+}
+
+static int perf__doh_close(struct perf_net_socket* sock)
+{
+ // TODO
+ return close(sock->fd);
+}
+
+static int perf__doh_sockeq(struct perf_net_socket* sock_a, struct perf_net_socket* sock_b)
+{
+ return sock_a->fd == sock_b->fd;
+}
+
+static int perf__doh_sockready(struct perf_net_socket* sock, int pipe_fd, int64_t timeout)
+{
+ PERF_LOCK(&self->lock);
+
+ if (self->do_reconnect) {
+ perf__doh_reconnect(sock);
+ self->do_reconnect = false;
+ }
+ if (self->do_connect) {
+ perf__doh_connect(sock);
+ self->do_connect = false;
+ }
+
+ if (self->is_ready) {
+ // do nghttp2 I/O send to flush outstanding frames
+ int ret = nghttp2_session_send(self->http2.session);
+ if (ret != 0) {
+ // TODO: handle error better, reconnect when needed
+ perf_log_warning("nghttp2_session_send failed: %s", nghttp2_strerror(ret));
+ self->do_reconnect = true;
+ PERF_UNLOCK(&self->lock);
+ return 0;
+ }
+
+ bool sent = false;
+ if (self->is_sending) {
+ if (self->is_post_sending || nghttp2_session_get_outbound_queue_size(self->http2.session) > 0 || nghttp2_session_want_write(self->http2.session)) {
+ PERF_UNLOCK(&self->lock);
+ return 0;
+ }
+ self->is_sending = false;
+ sent = true;
+ self->nqpc_sent++;
+ }
+ if (self->num_queries_per_conn && self->nqpc_sent >= self->num_queries_per_conn) {
+ if (!self->nqpc_ts) {
+ self->nqpc_ts = perf_get_time() + self->nqpc_timeout;
+ }
+ unsigned int r = ck_pr_load_uint(&self->nqpc_recv);
+ if (r >= self->nqpc_sent || perf_get_time() > self->nqpc_ts) {
+ self->do_reconnect = true;
+ }
+ PERF_UNLOCK(&self->lock);
+ if (sent && sock->sent) {
+ sock->sent(sock, self->qid);
+ }
+ return 0;
+ }
+ PERF_UNLOCK(&self->lock);
+ if (sent && sock->sent) {
+ sock->sent(sock, self->qid);
+ }
+ return 1;
+ }
+
+ if (!self->is_conn_ready) {
+ switch (perf_os_waituntilanywritable(&sock, 1, pipe_fd, timeout)) {
+ case PERF_R_TIMEDOUT:
+ PERF_UNLOCK(&self->lock);
+ return -1;
+ case PERF_R_SUCCESS: {
+ int error = 0;
+ socklen_t len = (socklen_t)sizeof(error);
+
+ getsockopt(sock->fd, SOL_SOCKET, SO_ERROR, (void*)&error, &len);
+ if (error != 0) {
+ if (error == EINPROGRESS
+#if EWOULDBLOCK != EAGAIN
+ || error == EWOULDBLOCK
+#endif
+ || error == EAGAIN) {
+ PERF_UNLOCK(&self->lock);
+ return 0;
+ }
+ // unrecoverable error, reconnect
+ self->do_reconnect = true;
+ PERF_UNLOCK(&self->lock);
+ return 0;
+ }
+ break;
+ }
+ default:
+ PERF_UNLOCK(&self->lock);
+ return -1;
+ }
+ self->is_conn_ready = true;
+ }
+
+ if (!self->is_ssl_ready) {
+ int ret = SSL_connect(self->ssl);
+ if (!ret) {
+ // unrecoverable error, reconnect
+ self->do_reconnect = true;
+ PERF_UNLOCK(&self->lock);
+ return 0;
+ }
+ if (ret < 0) {
+ switch (SSL_get_error(self->ssl, ret)) {
+ case SSL_ERROR_WANT_READ:
+ case SSL_ERROR_WANT_WRITE:
+ break;
+ default:
+ // unrecoverable error, reconnect
+ self->do_reconnect = true;
+ }
+ PERF_UNLOCK(&self->lock);
+ return 0;
+ }
+
+ const uint8_t* alpn = 0;
+ uint32_t alpn_len = 0;
+#ifndef OPENSSL_NO_NEXTPROTONEG
+ SSL_get0_next_proto_negotiated(self->ssl, &alpn, &alpn_len);
+#endif /* !OPENSSL_NO_NEXTPROTONEG */
+#if OPENSSL_VERSION_NUMBER >= 0x10002000L
+ if (!alpn) {
+ SSL_get0_alpn_selected(self->ssl, &alpn, &alpn_len);
+ }
+#endif /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
+#if defined(OPENSSL_NO_NEXTPROTONEG) && OPENSSL_VERSION_NUMBER < 0x10002000L
+#error "OpenSSL has no support for getting alpn"
+#endif
+ if (!alpn || alpn_len != 2 || memcmp("h2", alpn, 2) != 0) {
+ perf_log_warning("Unable to get ALPN or not \"h2\", reconnecting");
+ self->do_reconnect = true;
+ PERF_UNLOCK(&self->lock);
+ return 0;
+ }
+ self->is_ssl_ready = true;
+ }
+
+ if (!self->http2.settings_sent) {
+ // send settings
+ // TODO: does sending settings need session_send()?
+ nghttp2_settings_entry iv[] = {
+ { NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, doh_max_concurr },
+ { NGHTTP2_SETTINGS_ENABLE_PUSH, 0 },
+ { NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, 65535 }
+ };
+ int ret = nghttp2_submit_settings(self->http2.session, NGHTTP2_FLAG_NONE, iv,
+ sizeof(iv) / sizeof(*iv));
+ if (ret != 0) {
+ perf_log_warning("Could not submit https2 SETTINGS: %s", nghttp2_strerror(ret));
+ self->do_reconnect = true;
+ PERF_UNLOCK(&self->lock);
+ return 0;
+ }
+ self->http2.settings_sent = true;
+ }
+
+ self->is_ready = true;
+ PERF_UNLOCK(&self->lock);
+
+ if (sock->event) {
+ sock->event(sock, self->conn_event, perf_get_time() - self->conn_ts);
+ self->conn_event = perf_socket_event_reconnected;
+ }
+
+ return 1;
+}
+
+static bool perf__doh_have_more(struct perf_net_socket* sock)
+{
+ // return self->have_more; TODO
+ return false;
+}
+
+/* nghttp2 callbacks */
+
+static ssize_t _http2_send_cb(nghttp2_session* session,
+ const uint8_t* data,
+ size_t length,
+ int flags,
+ void* sock)
+{
+ // TODO: remove once non-experimental
+ if (!PERF_TRYLOCK(&self->lock)) {
+ perf_log_fatal("_http2_send_cb called without lock");
+ }
+
+ if (!self->is_ready) {
+ return NGHTTP2_ERR_CALLBACK_FAILURE;
+ }
+
+ ssize_t n = SSL_write(self->ssl, data, length);
+ if (n < 1) {
+ switch (SSL_get_error(self->ssl, n)) {
+ case SSL_ERROR_SYSCALL:
+ switch (errno) {
+ case ECONNREFUSED:
+ case ECONNRESET:
+ case ENOTCONN:
+ case EPIPE:
+ perf__doh_reconnect(sock);
+ return NGHTTP2_ERR_CALLBACK_FAILURE;
+ default:
+ break;
+ }
+ break;
+ case SSL_ERROR_WANT_READ:
+ case SSL_ERROR_WANT_WRITE:
+ return NGHTTP2_ERR_WOULDBLOCK;
+ default:
+ break;
+ }
+ perf_log_warning("SSL_write(): %s", ERR_error_string(SSL_get_error(self->ssl, n), 0));
+ return NGHTTP2_ERR_CALLBACK_FAILURE;
+ }
+
+ return n;
+}
+
+static int _http2_frame_recv_cb(nghttp2_session* session, const nghttp2_frame* frame, void* sock)
+{
+ // TODO: remove once non-experimental
+ if (!PERF_TRYLOCK(&self->lock)) {
+ perf_log_fatal("_http2_frame_recv_cb called without lock");
+ }
+
+ switch (frame->hd.type) {
+ case NGHTTP2_DATA:
+ // we are interested in DATA frame which will carry the DNS response
+ // NGHTTP2_FLAG_END_STREAM indicates that we have the data in full
+ if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
+ // TODO: what's the point of below code? if dnsmsg_at > max size then it will already done a buffer overflow
+ // if (self->http2.dnsmsg_at > DNS_MSG_MAX_SIZE) {
+ // perf_log_warning("DNS response > DNS message maximum size");
+ // return NGHTTP2_ERR_CALLBACK_FAILURE;
+ // }
+
+ // TODO: need to be able to receive multiple responses at the same time
+ if (self->http2.dnsmsg_completed) {
+ perf_log_fatal("_http2_frame_recv_cb: frame received when already having a dns msg");
+ }
+
+ self->http2.dnsmsg_completed = true;
+ // self->have_more = false; TODO
+ }
+ break;
+ case NGHTTP2_RST_STREAM:
+ case NGHTTP2_GOAWAY:
+ perf__doh_reconnect(sock);
+ break;
+ default:
+ break;
+ }
+
+ return 0;
+}
+
+static int _http2_on_header_callback(nghttp2_session* session, const nghttp2_frame* frame, const uint8_t* name, size_t namelen, const uint8_t* value, size_t valuelen, uint8_t flags, void* sock)
+{
+ switch (frame->hd.type) {
+ case NGHTTP2_HEADERS: {
+ if (!value) {
+ return 0;
+ }
+ if (!strncasecmp(":status:", (const char*)name, namelen)) {
+ self->http2_code = atoi((const char*)value);
+ } else if (!strncasecmp("content-type:", (const char*)name, namelen)) {
+ if (!strncasecmp("application/dns-message", (const char*)value, valuelen)) {
+ self->http2_is_dns = true;
+ }
+ }
+ break;
+ }
+ default:
+ break;
+ }
+ return 0;
+}
+
+static int _http2_data_chunk_recv_cb(nghttp2_session* session,
+ uint8_t flags,
+ int32_t stream_id,
+ const uint8_t* data,
+ size_t len, void* sock)
+{
+ // TODO: remove once non-experimental
+ if (!PERF_TRYLOCK(&self->lock)) {
+ perf_log_fatal("_http2_data_chunk_recv_cb called without lock");
+ }
+
+ if (self->http2.dnsmsg_completed) {
+ // pause for now and don't process this chunk
+ // can only do one response at a time in perf__doh_recv()
+ return NGHTTP2_ERR_PAUSE;
+ }
+
+ // TODO: point of nghttp2_session_get_stream_user_data() code?
+ // if (nghttp2_session_get_stream_user_data(session, stream_id)) {
+ if (self->http2.dnsmsg_at + len > DNS_MSG_MAX_SIZE) {
+ perf_log_warning("http2 chunk data exceeds DNS message max size");
+ return NGHTTP2_ERR_CALLBACK_FAILURE;
+ }
+ memcpy(self->http2.dnsmsg + self->http2.dnsmsg_at, data, len);
+ self->http2.dnsmsg_at += len;
+ // }
+
+ return 0;
+}
+
+#ifndef OPENSSL_NO_NEXTPROTONEG
+/* NPN TLS extension check */
+static int select_next_proto_cb(SSL* ssl, unsigned char** out,
+ unsigned char* outlen, const unsigned char* in,
+ unsigned int inlen, void* arg)
+{
+ if (nghttp2_select_next_protocol(out, outlen, in, inlen) <= 0) {
+ perf_log_warning("Server did not advertise %u", NGHTTP2_PROTO_VERSION_ID);
+ return SSL_TLSEXT_ERR_ALERT_WARNING;
+ }
+
+ return SSL_TLSEXT_ERR_OK;
+}
+#endif /* !OPENSSL_NO_NEXTPROTONEG */
+
+static void perf__doh_num_queries_per_conn(struct perf_net_socket* sock, size_t num_queries_per_conn, size_t timeout)
+{
+ self->num_queries_per_conn = num_queries_per_conn;
+ self->nqpc_timeout = timeout;
+}
+
+struct perf_net_socket* perf_net_doh_opensocket(const perf_sockaddr_t* server, const perf_sockaddr_t* local, size_t bufsize, void* data, perf_net_sent_cb_t sent, perf_net_event_cb_t event)
+{
+ struct perf__doh_socket* tmp = calloc(1, sizeof(struct perf__doh_socket)); // clang scan-build
+ struct perf_net_socket* sock = (struct perf_net_socket*)tmp;
+
+ if (!sock) {
+ perf_log_fatal("perf_net_doh_opensocket(): out of memory");
+ return 0; // needed for clang scan build
+ }
+
+ sock->recv = perf__doh_recv;
+ sock->sendto = perf__doh_sendto;
+ sock->close = perf__doh_close;
+ sock->sockeq = perf__doh_sockeq;
+ sock->sockready = perf__doh_sockready;
+ sock->have_more = perf__doh_have_more;
+
+ sock->num_queries_per_conn = perf__doh_num_queries_per_conn;
+
+ sock->data = data;
+ sock->sent = sent;
+ sock->event = event;
+
+ self->server = *server;
+ self->local = *local;
+ self->bufsize = bufsize;
+ if (self->bufsize > 0) {
+ self->bufsize *= 1024;
+ }
+ self->conning_event = perf_socket_event_connecting;
+ self->conn_event = perf_socket_event_connected;
+ PERF_MUTEX_INIT(&self->lock);
+
+ if (!(self->http2.payload.buf = malloc(MAX_EDNS_PACKET))) {
+ perf_log_fatal("perf_net_doh_opensocket(): out of memory");
+ }
+ self->http2.payload.buf_len = MAX_EDNS_PACKET;
+
+ if (!ssl_ctx) {
+#ifdef HAVE_TLS_METHOD
+ if (!(ssl_ctx = SSL_CTX_new(TLS_method()))) {
+ perf_log_fatal("SSL_CTX_new(): %s", ERR_error_string(ERR_get_error(), 0));
+ }
+ if (!SSL_CTX_set_min_proto_version(ssl_ctx, TLS1_2_VERSION)) {
+ perf_log_fatal("SSL_CTX_set_min_proto_version(TLS1_2_VERSION): %s", ERR_error_string(ERR_get_error(), 0));
+ }
+#else
+ if (!(ssl_ctx = SSL_CTX_new(SSLv23_client_method()))) {
+ perf_log_fatal("SSL_CTX_new(): %s", ERR_error_string(ERR_get_error(), 0));
+ }
+#endif
+ SSL_CTX_set_mode(ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
+#ifndef OPENSSL_NO_NEXTPROTONEG
+ SSL_CTX_set_next_proto_select_cb(ssl_ctx, select_next_proto_cb, NULL);
+#endif /* !OPENSSL_NO_NEXTPROTONEG */
+#if OPENSSL_VERSION_NUMBER >= 0x10002000L
+ SSL_CTX_set_alpn_protos(ssl_ctx, (const unsigned char*)"\x02h2", 3);
+#endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
+ }
+
+ /* setup HTTP/2 callbacks */
+ if (!_nghttp2_callbacks || !_nghttp2_option) {
+ PERF_LOCK(&_nghttp2_lock);
+ if (!_nghttp2_callbacks) {
+ if (nghttp2_session_callbacks_new(&_nghttp2_callbacks)) {
+ perf_log_fatal("Unable to create nghttp2 callbacks: out of memory");
+ }
+ nghttp2_session_callbacks_set_send_callback(_nghttp2_callbacks, _http2_send_cb);
+ nghttp2_session_callbacks_set_on_data_chunk_recv_callback(_nghttp2_callbacks, _http2_data_chunk_recv_cb);
+ nghttp2_session_callbacks_set_on_frame_recv_callback(_nghttp2_callbacks, _http2_frame_recv_cb);
+ nghttp2_session_callbacks_set_on_header_callback(_nghttp2_callbacks, _http2_on_header_callback);
+
+ // nghttp2_session_callbacks_set_recv_callback(_nghttp2_callbacks, _recv_callback);
+ }
+
+ /* setup HTTP/2 options */
+ if (!_nghttp2_option) {
+ if (nghttp2_option_new(&_nghttp2_option)) {
+ perf_log_fatal("Unable to create nghttp2 options: out of memory");
+ }
+ nghttp2_option_set_peer_max_concurrent_streams(_nghttp2_option, doh_max_concurr);
+ }
+ PERF_UNLOCK(&_nghttp2_lock);
+ }
+
+ perf__doh_connect(sock);
+
+ return sock;
+}
+
+static struct _doh_stats doh_stats;
+
+void perf_net_doh_stats_init()
+{
+ memset(&doh_stats, 0, sizeof(doh_stats));
+}
+
+void perf_net_doh_stats_compile(struct perf_net_socket* sock)
+{
+ int i;
+ for (i = 0; i < (sizeof(doh_stats.code) / sizeof(doh_stats.code[0])); i++) {
+ doh_stats.code[i] += self->stats.code[i];
+ }
+ doh_stats.unknown_code += self->stats.unknown_code;
+}
+
+void perf_net_doh_stats_print(bool json)
+{
+#ifdef HAVE_JSON_C
+ if (json) {
+ int ret = 0;
+
+ struct json_object* dns_over_https = json_object_new_object();
+
+ struct json_object* status_codes = json_object_new_object();
+ int i;
+ char code[16];
+ for (i = 0; i < (sizeof(doh_stats.code) / sizeof(doh_stats.code[0])); i++) {
+ if (doh_stats.code[i]) {
+ snprintf(code, sizeof(code), "%d", i + 100);
+ ret |= json_object_object_add(status_codes, code, json_object_new_uint64(doh_stats.code[i]));
+ }
+ }
+ if (doh_stats.unknown_code) {
+ ret |= json_object_object_add(status_codes, "unknown", json_object_new_uint64(doh_stats.unknown_code));
+ }
+ ret |= json_object_object_add(dns_over_https, "status_codes", status_codes);
+
+ struct json_object* json = json_object_new_object();
+ ret |= json_object_object_add(json, "dns_over_https", dns_over_https);
+ if (!ret) {
+ printf("%s\n", json_object_to_json_string_ext(json, JSON_C_TO_STRING_NOSLASHESCAPE));
+ } else {
+ perf_log_fatal("unable to construct JSON output");
+ }
+ json_object_put(json);
+ return;
+ }
+#endif
+
+ printf("DNS-over-HTTPS statistics:\n\n");
+
+ printf(" HTTP/2 return codes: ");
+ bool first_code = true, no_codes = true;
+ int i;
+ for (i = 0; i < (sizeof(doh_stats.code) / sizeof(doh_stats.code[0])); i++) {
+ if (doh_stats.code[i]) {
+ if (first_code)
+ first_code = false;
+ else
+ printf(", ");
+ printf("%d: %zu", i + 100, doh_stats.code[i]);
+ no_codes = false;
+ }
+ }
+ if (doh_stats.unknown_code) {
+ if (!first_code)
+ printf(", ");
+ printf("unknown: %zu", doh_stats.unknown_code);
+ no_codes = false;
+ }
+ if (no_codes) {
+ printf("none");
+ }
+ printf("\n");
+
+ printf("\n");
+}
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "config.h"
+
+#include "net.h"
+
+#include "log.h"
+#include "strerror.h"
+#include "util.h"
+#include "os.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <openssl/err.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <ck_pr.h>
+
+static SSL_CTX* ssl_ctx = 0;
+
+#define self ((struct perf__dot_socket*)sock)
+
+struct perf__dot_socket {
+ struct perf_net_socket base;
+
+ pthread_mutex_t lock;
+ SSL* ssl;
+
+ char recvbuf[TCP_RECV_BUF_SIZE], sendbuf[TCP_SEND_BUF_SIZE];
+ size_t at, sending;
+ bool is_ready, is_conn_ready, have_more, is_sending, do_reconnect;
+
+ perf_sockaddr_t server, local;
+ size_t bufsize;
+
+ uint16_t qid;
+
+ uint64_t conn_ts;
+ perf_socket_event_t conn_event, conning_event;
+
+ size_t num_queries_per_conn, nqpc_timeout;
+ unsigned int nqpc_sent, nqpc_recv;
+ uint64_t nqpc_ts;
+};
+
+static void perf__dot_connect(struct perf_net_socket* sock)
+{
+ int ret;
+
+ self->nqpc_sent = 0;
+ ck_pr_store_uint(&self->nqpc_recv, 0);
+ self->nqpc_ts = 0;
+
+ int fd = socket(self->server.sa.sa.sa_family, SOCK_STREAM, 0);
+ if (fd == -1) {
+ char __s[256];
+ perf_log_fatal("socket: %s", perf_strerror_r(errno, __s, sizeof(__s)));
+ }
+ ck_pr_store_int(&sock->fd, fd);
+
+ if (self->ssl) {
+ SSL_free(self->ssl);
+ }
+ if (!(self->ssl = SSL_new(ssl_ctx))) {
+ perf_log_fatal("SSL_new(): %s", ERR_error_string(ERR_get_error(), 0));
+ }
+ if (perf_net_tls_sni && !(ret = SSL_set_tlsext_host_name(self->ssl, perf_net_tls_sni))) {
+ perf_log_fatal("SSL_set_tlsext_host_name(): %s", ERR_error_string(SSL_get_error(self->ssl, ret), 0));
+ }
+ if (!(ret = SSL_set_fd(self->ssl, sock->fd))) {
+ perf_log_fatal("SSL_set_fd(): %s", ERR_error_string(SSL_get_error(self->ssl, ret), 0));
+ }
+
+ if (self->server.sa.sa.sa_family == AF_INET6) {
+ int on = 1;
+
+ if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) == -1) {
+ perf_log_warning("setsockopt(IPV6_V6ONLY) failed");
+ }
+ }
+
+ if (bind(sock->fd, &self->local.sa.sa, self->local.length) == -1) {
+ char __s[256];
+ perf_log_fatal("bind: %s", perf_strerror_r(errno, __s, sizeof(__s)));
+ }
+
+ if (self->bufsize > 0) {
+ ret = setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
+ &self->bufsize, sizeof(self->bufsize));
+ if (ret < 0)
+ perf_log_warning("setsockbuf(SO_RCVBUF) failed");
+
+ ret = setsockopt(sock->fd, SOL_SOCKET, SO_SNDBUF,
+ &self->bufsize, sizeof(self->bufsize));
+ if (ret < 0)
+ perf_log_warning("setsockbuf(SO_SNDBUF) failed");
+ }
+
+ int flags = fcntl(sock->fd, F_GETFL, 0);
+ if (flags < 0)
+ perf_log_fatal("fcntl(F_GETFL)");
+ ret = fcntl(sock->fd, F_SETFL, flags | O_NONBLOCK);
+ if (ret < 0)
+ perf_log_fatal("fcntl(F_SETFL)");
+
+ self->conn_ts = perf_get_time();
+ if (sock->event) {
+ sock->event(sock, self->conning_event, self->conn_ts);
+ self->conning_event = perf_socket_event_reconnecting;
+ }
+ if (connect(sock->fd, &self->server.sa.sa, self->server.length)) {
+ if (errno == EINPROGRESS) {
+ return;
+ } else {
+ char __s[256];
+ perf_log_fatal("connect() failed: %s", perf_strerror_r(errno, __s, sizeof(__s)));
+ }
+ }
+
+ self->is_conn_ready = true;
+}
+
+static void perf__dot_reconnect(struct perf_net_socket* sock)
+{
+ close(sock->fd);
+ self->have_more = false;
+ self->at = 0;
+ if (self->sending) {
+ self->sending = 0;
+ self->is_sending = false;
+ }
+ self->is_ready = false;
+ self->is_conn_ready = false;
+ perf__dot_connect(sock);
+}
+
+static ssize_t perf__dot_recv(struct perf_net_socket* sock, void* buf, size_t len, int flags)
+{
+ ssize_t n;
+ uint16_t dnslen, dnslen2;
+
+ if (!self->have_more) {
+ PERF_LOCK(&self->lock);
+ if (!self->is_ready) {
+ PERF_UNLOCK(&self->lock);
+ errno = EAGAIN;
+ return -1;
+ }
+
+ n = SSL_read(self->ssl, self->recvbuf + self->at, TCP_RECV_BUF_SIZE - self->at);
+ if (!n) {
+ perf__dot_reconnect(sock);
+ PERF_UNLOCK(&self->lock);
+ errno = EAGAIN;
+ return -1;
+ }
+ if (n < 0) {
+ int err = SSL_get_error(self->ssl, n);
+ switch (err) {
+ case SSL_ERROR_WANT_READ:
+ errno = EAGAIN;
+ break;
+#if OPENSSL_VERSION_NUMBER > 0x30000000L
+ case SSL_ERROR_SSL:
+ // OpenSSL 3.0+ returns this on EOF, treat everything as bad fd and reconnect
+ errno = EBADF;
+#endif
+ case SSL_ERROR_SYSCALL:
+ switch (errno) {
+ case EBADF:
+ // treat this as a retry, can happen if sendto is reconnecting
+ case ECONNREFUSED:
+ case ECONNRESET:
+ case ENOTCONN:
+ perf__dot_reconnect(sock);
+ errno = EAGAIN;
+ break;
+ default:
+ break;
+ }
+ break;
+ default:
+ errno = EBADF;
+ break;
+ }
+ PERF_UNLOCK(&self->lock);
+ return -1;
+ }
+ PERF_UNLOCK(&self->lock);
+
+ self->at += n;
+ if (self->at < 3) {
+ errno = EAGAIN;
+ return -1;
+ }
+ }
+
+ memcpy(&dnslen, self->recvbuf, 2);
+ dnslen = ntohs(dnslen);
+ if (self->at < dnslen + 2) {
+ errno = EAGAIN;
+ return -1;
+ }
+ memcpy(buf, self->recvbuf + 2, len < dnslen ? len : dnslen);
+ memmove(self->recvbuf, self->recvbuf + 2 + dnslen, self->at - 2 - dnslen);
+ self->at -= 2 + dnslen;
+ if (self->num_queries_per_conn) {
+ ck_pr_inc_uint(&self->nqpc_recv);
+ }
+
+ if (self->at > 2) {
+ memcpy(&dnslen2, self->recvbuf, 2);
+ dnslen2 = ntohs(dnslen2);
+ if (self->at >= dnslen2 + 2) {
+ self->have_more = true;
+ return dnslen;
+ }
+ }
+
+ self->have_more = false;
+ return dnslen;
+}
+
+static ssize_t perf__dot_sendto(struct perf_net_socket* sock, uint16_t qid, const void* buf, size_t len, int flags, const struct sockaddr* dest_addr, socklen_t addrlen)
+{
+ size_t send = len < TCP_SEND_BUF_SIZE - 2 ? len : (TCP_SEND_BUF_SIZE - 2);
+ // TODO: We only send what we can send, because we can't continue sending
+ uint16_t dnslen = htons(send);
+ ssize_t n;
+
+ PERF_LOCK(&self->lock);
+
+ memcpy(self->sendbuf, &dnslen, 2);
+ memcpy(self->sendbuf + 2, buf, send);
+ self->qid = qid;
+
+ if (!self->is_ready) {
+ self->is_sending = true;
+ self->sending = 0;
+ PERF_UNLOCK(&self->lock);
+ errno = EINPROGRESS;
+ return -1;
+ }
+
+ n = SSL_write(self->ssl, self->sendbuf, send + 2);
+ if (n < 1) {
+ switch (SSL_get_error(self->ssl, n)) {
+ case SSL_ERROR_SYSCALL:
+ switch (errno) {
+ case ECONNREFUSED:
+ case ECONNRESET:
+ case ENOTCONN:
+ case EPIPE:
+ perf__dot_reconnect(sock);
+ self->is_sending = true;
+ self->sending = 0;
+ PERF_UNLOCK(&self->lock);
+ errno = EINPROGRESS;
+ return -1;
+ default:
+ break;
+ }
+ PERF_UNLOCK(&self->lock);
+ return -1;
+ case SSL_ERROR_WANT_READ:
+ case SSL_ERROR_WANT_WRITE:
+ self->is_sending = true;
+ self->sending = 0;
+ PERF_UNLOCK(&self->lock);
+ errno = EINPROGRESS;
+ return -1;
+ default:
+ break;
+ }
+ perf_log_warning("SSL_write(): %s", ERR_error_string(SSL_get_error(self->ssl, n), 0));
+ errno = EBADF;
+ return -1;
+ }
+
+ if (n < send + 2) {
+ self->sending = n;
+ self->is_sending = true;
+ PERF_UNLOCK(&self->lock);
+ errno = EINPROGRESS;
+ return -1;
+ }
+ PERF_UNLOCK(&self->lock);
+
+ self->nqpc_sent++;
+
+ return n - 2;
+}
+
+static int perf__dot_close(struct perf_net_socket* sock)
+{
+ // TODO
+ return close(sock->fd);
+}
+
+static int perf__dot_sockeq(struct perf_net_socket* sock_a, struct perf_net_socket* sock_b)
+{
+ return sock_a->fd == sock_b->fd;
+}
+
+static int perf__dot_sockready(struct perf_net_socket* sock, int pipe_fd, int64_t timeout)
+{
+ PERF_LOCK(&self->lock);
+ if (self->do_reconnect) {
+ perf__dot_reconnect(sock);
+ self->do_reconnect = false;
+ }
+
+ if (self->is_ready) {
+ if (self->is_sending) {
+ uint16_t dnslen;
+ ssize_t n;
+
+ memcpy(&dnslen, self->sendbuf, 2);
+ dnslen = ntohs(dnslen);
+
+ n = SSL_write(self->ssl, self->sendbuf + self->sending, dnslen + 2 - self->sending);
+ if (n < 1) {
+ switch (SSL_get_error(self->ssl, n)) {
+ case SSL_ERROR_SYSCALL:
+ perf__dot_reconnect(sock);
+ PERF_UNLOCK(&self->lock);
+ return 0;
+ case SSL_ERROR_WANT_READ:
+ case SSL_ERROR_WANT_WRITE:
+ PERF_UNLOCK(&self->lock);
+ return 0;
+ default:
+ break;
+ }
+ perf_log_warning("SSL_write(): %s", ERR_error_string(SSL_get_error(self->ssl, n), 0));
+ PERF_UNLOCK(&self->lock);
+ return 0;
+ }
+ PERF_UNLOCK(&self->lock);
+
+ self->sending += n;
+ if (self->sending < dnslen + 2) {
+ return 0;
+ }
+ self->sending = 0;
+ self->is_sending = false;
+ if (sock->sent) {
+ sock->sent(sock, self->qid);
+ }
+ self->nqpc_sent++;
+ } else {
+ PERF_UNLOCK(&self->lock);
+ }
+ if (self->num_queries_per_conn && self->nqpc_sent >= self->num_queries_per_conn) {
+ if (!self->nqpc_ts) {
+ self->nqpc_ts = perf_get_time() + self->nqpc_timeout;
+ }
+ unsigned int r = ck_pr_load_uint(&self->nqpc_recv);
+ if (r >= self->nqpc_sent || perf_get_time() > self->nqpc_ts) {
+ PERF_LOCK(&self->lock);
+ perf__dot_reconnect(sock);
+ PERF_UNLOCK(&self->lock);
+ }
+ return 0;
+ }
+ return 1;
+ }
+
+ if (!self->is_conn_ready) {
+ switch (perf_os_waituntilanywritable(&sock, 1, pipe_fd, timeout)) {
+ case PERF_R_TIMEDOUT:
+ PERF_UNLOCK(&self->lock);
+ return -1;
+ case PERF_R_SUCCESS: {
+ int error = 0;
+ socklen_t len = (socklen_t)sizeof(error);
+
+ getsockopt(sock->fd, SOL_SOCKET, SO_ERROR, (void*)&error, &len);
+ if (error != 0) {
+ if (error == EINPROGRESS
+#if EWOULDBLOCK != EAGAIN
+ || error == EWOULDBLOCK
+#endif
+ || error == EAGAIN) {
+ PERF_UNLOCK(&self->lock);
+ return 0;
+ }
+ // unrecoverable error, reconnect
+ self->do_reconnect = true;
+ PERF_UNLOCK(&self->lock);
+ return 0;
+ }
+ break;
+ }
+ default:
+ PERF_UNLOCK(&self->lock);
+ return -1;
+ }
+ self->is_conn_ready = true;
+ }
+
+ int ret = SSL_connect(self->ssl);
+ if (!ret) {
+ // unrecoverable error, reconnect
+ self->do_reconnect = true;
+ PERF_UNLOCK(&self->lock);
+ return 0;
+ }
+ if (ret < 0) {
+ switch (SSL_get_error(self->ssl, ret)) {
+ case SSL_ERROR_WANT_READ:
+ case SSL_ERROR_WANT_WRITE:
+ break;
+ default:
+ // unrecoverable error, reconnect
+ self->do_reconnect = true;
+ }
+ PERF_UNLOCK(&self->lock);
+ return 0;
+ }
+ self->is_ready = true;
+ PERF_UNLOCK(&self->lock);
+ if (sock->event) {
+ sock->event(sock, self->conn_event, perf_get_time() - self->conn_ts);
+ self->conn_event = perf_socket_event_reconnected;
+ }
+ if (self->is_sending) {
+ return 0;
+ }
+ return 1;
+}
+
+static bool perf__dot_have_more(struct perf_net_socket* sock)
+{
+ return self->have_more;
+}
+
+static void perf__dot_num_queries_per_conn(struct perf_net_socket* sock, size_t num_queries_per_conn, size_t timeout)
+{
+ self->num_queries_per_conn = num_queries_per_conn;
+ self->nqpc_timeout = timeout;
+}
+
+struct perf_net_socket* perf_net_dot_opensocket(const perf_sockaddr_t* server, const perf_sockaddr_t* local, size_t bufsize, void* data, perf_net_sent_cb_t sent, perf_net_event_cb_t event)
+{
+ struct perf__dot_socket* tmp = calloc(1, sizeof(struct perf__dot_socket)); // clang scan-build
+ struct perf_net_socket* sock = (struct perf_net_socket*)tmp;
+
+ if (!sock) {
+ perf_log_fatal("perf_net_dot_opensocket() out of memory");
+ return 0; // needed for clang scan build
+ }
+
+ sock->recv = perf__dot_recv;
+ sock->sendto = perf__dot_sendto;
+ sock->close = perf__dot_close;
+ sock->sockeq = perf__dot_sockeq;
+ sock->sockready = perf__dot_sockready;
+ sock->have_more = perf__dot_have_more;
+
+ sock->num_queries_per_conn = perf__dot_num_queries_per_conn;
+
+ sock->data = data;
+ sock->sent = sent;
+ sock->event = event;
+
+ self->server = *server;
+ self->local = *local;
+ self->bufsize = bufsize;
+ if (self->bufsize > 0) {
+ self->bufsize *= 1024;
+ }
+ self->conning_event = perf_socket_event_connecting;
+ self->conn_event = perf_socket_event_connected;
+ PERF_MUTEX_INIT(&self->lock);
+
+ if (!ssl_ctx) {
+#ifdef HAVE_TLS_METHOD
+ if (!(ssl_ctx = SSL_CTX_new(TLS_method()))) {
+ perf_log_fatal("SSL_CTX_new(): %s", ERR_error_string(ERR_get_error(), 0));
+ }
+ if (!SSL_CTX_set_min_proto_version(ssl_ctx, TLS1_2_VERSION)) {
+ perf_log_fatal("SSL_CTX_set_min_proto_version(TLS1_2_VERSION): %s", ERR_error_string(ERR_get_error(), 0));
+ }
+#else
+ if (!(ssl_ctx = SSL_CTX_new(SSLv23_client_method()))) {
+ perf_log_fatal("SSL_CTX_new(): %s", ERR_error_string(ERR_get_error(), 0));
+ }
+#endif
+ SSL_CTX_set_mode(ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
+ }
+
+ perf__dot_connect(sock);
+
+ return sock;
+}
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "config.h"
+
+#include "net.h"
+
+#include "log.h"
+#include "strerror.h"
+#include "os.h"
+#include "util.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <ck_pr.h>
+
+#define self ((struct perf__tcp_socket*)sock)
+
+/*
+
+About state sync between sending and receiving thread
+
+Two variables in the TCP socket struct are used with libck to atomically
+sync states between thread w.r.t connect/reconnect events.
+
+ sock->fd is controlled by the sending thread (ST)
+ self->recv_need_reconn is controlled by receiving thread (RT)
+
+On connect/reconnect ST will open a new socket and atomically store it
+into sock->fd.
+
+When RT is trying to receive it will atomically load sock->fd and store it
+in self->recvfd. Before storing it, it will compare it to what already in
+self->recvfd and if it differ then a connect/reconnect event happend and RT
+will reset receiving state and buffers.
+
+If RT detects a disconnection it will atomically store self->recvfd into
+self->recv_need_reconn to signal to ST that it needs to reconnect.
+
+ST will load and check self->recv_need_reconn before sending and when
+checking socket readiness, if its the same as sock->fd then it will start
+reconnecting.
+
+*/
+
+struct perf__tcp_socket {
+ struct perf_net_socket base;
+
+ char recvbuf[TCP_RECV_BUF_SIZE], sendbuf[TCP_SEND_BUF_SIZE];
+ size_t at, sending;
+ bool is_ready, need_reconnect, have_more, is_sending;
+
+ perf_sockaddr_t server, local;
+ size_t bufsize;
+
+ int recvfd;
+ int recv_need_reconn;
+
+ uint16_t qid;
+
+ uint64_t conn_ts;
+ perf_socket_event_t conn_event, conning_event;
+
+ size_t num_queries_per_conn, nqpc_timeout;
+ unsigned int nqpc_sent, nqpc_recv;
+ uint64_t nqpc_ts;
+};
+
+static int perf__tcp_connect(struct perf_net_socket* sock)
+{
+ int fd;
+
+ self->is_ready = true;
+ self->nqpc_sent = 0;
+ ck_pr_store_uint(&self->nqpc_recv, 0);
+ self->nqpc_ts = 0;
+
+ fd = socket(self->server.sa.sa.sa_family, SOCK_STREAM, 0);
+ if (fd == -1) {
+ char __s[256];
+ perf_log_fatal("socket: %s", perf_strerror_r(errno, __s, sizeof(__s)));
+ }
+
+ if (self->server.sa.sa.sa_family == AF_INET6) {
+ int on = 1;
+
+ if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) == -1) {
+ perf_log_warning("setsockopt(IPV6_V6ONLY) failed");
+ }
+ }
+
+ if (bind(fd, &self->local.sa.sa, self->local.length) == -1) {
+ char __s[256];
+ perf_log_fatal("bind: %s", perf_strerror_r(errno, __s, sizeof(__s)));
+ }
+
+ if (self->bufsize) {
+ int ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
+ &self->bufsize, sizeof(self->bufsize));
+ if (ret < 0)
+ perf_log_warning("setsockbuf(SO_RCVBUF) failed");
+
+ ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
+ &self->bufsize, sizeof(self->bufsize));
+ if (ret < 0)
+ perf_log_warning("setsockbuf(SO_SNDBUF) failed");
+ }
+
+ int flags = fcntl(fd, F_GETFL, 0);
+ if (flags < 0)
+ perf_log_fatal("fcntl(F_GETFL)");
+ int ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
+ if (ret < 0)
+ perf_log_fatal("fcntl(F_SETFL)");
+
+ self->conn_ts = perf_get_time();
+ if (sock->event) {
+ sock->event(sock, self->conning_event, self->conn_ts);
+ self->conning_event = perf_socket_event_reconnecting;
+ }
+ if (connect(fd, &self->server.sa.sa, self->server.length)) {
+ if (errno == EINPROGRESS) {
+ self->is_ready = false;
+ } else {
+ char __s[256];
+ perf_log_fatal("connect() failed: %s", perf_strerror_r(errno, __s, sizeof(__s)));
+ }
+ }
+
+ return fd;
+}
+
+static ssize_t perf__tcp_recv(struct perf_net_socket* sock, void* buf, size_t len, int flags)
+{
+ ssize_t n;
+ uint16_t dnslen, dnslen2;
+
+ int fd = ck_pr_load_int(&sock->fd);
+ if (fd != self->recvfd) {
+ /* reconnecting happened, reset buffers */
+ self->have_more = false;
+ self->at = 0;
+ self->recvfd = fd;
+ }
+
+ if (!self->have_more) {
+ n = recv(fd, self->recvbuf + self->at, TCP_RECV_BUF_SIZE - self->at, flags);
+ if (!n) {
+ // need reconnect
+ ck_pr_store_int(&self->recv_need_reconn, fd);
+ return 0;
+ } else if (n < 0) {
+ switch (errno) {
+ case EBADF:
+ // treat this as a retry, can happen if sendto is reconnecting
+ case ECONNREFUSED:
+ case ECONNRESET:
+ case ENOTCONN:
+ // need reconnect
+ ck_pr_store_int(&self->recv_need_reconn, fd);
+ errno = EAGAIN;
+ break;
+ default:
+ break;
+ }
+ return n;
+ }
+ self->at += n;
+ if (self->at < 3) {
+ errno = EAGAIN;
+ return -1;
+ }
+ }
+
+ memcpy(&dnslen, self->recvbuf, 2);
+ dnslen = ntohs(dnslen);
+ if (self->at < dnslen + 2) {
+ errno = EAGAIN;
+ return -1;
+ }
+ memcpy(buf, self->recvbuf + 2, len < dnslen ? len : dnslen);
+ memmove(self->recvbuf, self->recvbuf + 2 + dnslen, self->at - 2 - dnslen);
+ self->at -= 2 + dnslen;
+ if (self->num_queries_per_conn) {
+ ck_pr_inc_uint(&self->nqpc_recv);
+ }
+
+ if (self->at > 2) {
+ memcpy(&dnslen2, self->recvbuf, 2);
+ dnslen2 = ntohs(dnslen2);
+ if (self->at >= dnslen2 + 2) {
+ self->have_more = true;
+ return dnslen;
+ }
+ }
+
+ self->have_more = false;
+ return dnslen;
+}
+
+static ssize_t perf__tcp_sendto(struct perf_net_socket* sock, uint16_t qid, const void* buf, size_t len, int flags, const struct sockaddr* dest_addr, socklen_t addrlen)
+{
+ size_t send = len < TCP_SEND_BUF_SIZE - 2 ? len : (TCP_SEND_BUF_SIZE - 2);
+ // TODO: We only send what we can send, because we can't continue sending
+ uint16_t dnslen = htons(send);
+ ssize_t n;
+
+ memcpy(self->sendbuf, &dnslen, 2);
+ memcpy(self->sendbuf + 2, buf, send);
+ self->qid = qid;
+
+ int recv_need_reconn = ck_pr_load_int(&self->recv_need_reconn);
+ if (recv_need_reconn == sock->fd) {
+ self->need_reconnect = true;
+ self->is_sending = true;
+ self->sending = 0;
+ errno = EINPROGRESS;
+ return -1;
+ }
+
+ n = sendto(sock->fd, self->sendbuf, send + 2, 0, 0, 0);
+
+ if (n < 0) {
+ switch (errno) {
+ case ECONNREFUSED:
+ case ECONNRESET:
+ case ENOTCONN:
+ case EPIPE:
+ self->need_reconnect = true;
+ self->is_sending = true;
+ self->sending = 0;
+ errno = EINPROGRESS;
+ return -1;
+ default:
+ break;
+ }
+ return -1;
+ }
+
+ if (n < send + 2) {
+ self->is_sending = true;
+ self->sending = n;
+ errno = EINPROGRESS;
+ return -1;
+ }
+ self->nqpc_sent++;
+
+ return n - 2;
+}
+
+static int perf__tcp_close(struct perf_net_socket* sock)
+{
+ return close(sock->fd);
+}
+
+static int perf__tcp_sockeq(struct perf_net_socket* sock_a, struct perf_net_socket* sock_b)
+{
+ return sock_a->fd == sock_b->fd;
+}
+
+static int perf__tcp_sockready(struct perf_net_socket* sock, int pipe_fd, int64_t timeout)
+{
+ int recv_need_reconn = ck_pr_load_int(&self->recv_need_reconn);
+ if (recv_need_reconn == sock->fd || self->need_reconnect) {
+ int fd = perf__tcp_connect(sock), oldfd = ck_pr_load_int(&sock->fd);
+ ck_pr_store_int(&sock->fd, fd);
+ close(oldfd);
+ self->need_reconnect = false;
+ }
+
+ if (self->is_ready) {
+ if (self->is_sending) {
+ uint16_t dnslen;
+ ssize_t n;
+
+ memcpy(&dnslen, self->sendbuf, 2);
+ dnslen = ntohs(dnslen);
+ n = sendto(sock->fd, self->sendbuf + self->sending, dnslen + 2 - self->sending, 0, 0, 0);
+ if (n < 0) {
+ if (errno == EAGAIN) {
+ return 0;
+ }
+ int fd = perf__tcp_connect(sock), oldfd = ck_pr_load_int(&sock->fd);
+ ck_pr_store_int(&sock->fd, fd);
+ close(oldfd);
+ if (self->sending) {
+ self->sending = 0;
+ self->is_sending = false;
+ }
+ goto conn_cont;
+ }
+ self->sending += n;
+ if (self->sending < dnslen + 2) {
+ return 0;
+ }
+ self->sending = 0;
+ self->is_sending = false;
+ if (sock->sent) {
+ sock->sent(sock, self->qid);
+ }
+ self->nqpc_sent++;
+ }
+ if (self->num_queries_per_conn && self->nqpc_sent >= self->num_queries_per_conn) {
+ if (!self->nqpc_ts) {
+ self->nqpc_ts = perf_get_time() + self->nqpc_timeout;
+ }
+ unsigned int r = ck_pr_load_uint(&self->nqpc_recv);
+ if (r >= self->nqpc_sent || perf_get_time() > self->nqpc_ts) {
+ self->need_reconnect = true;
+ }
+ return 0;
+ }
+ return 1;
+ }
+
+conn_cont:
+ switch (perf_os_waituntilanywritable(&sock, 1, pipe_fd, timeout)) {
+ case PERF_R_TIMEDOUT:
+ return -1;
+ case PERF_R_SUCCESS: {
+ int error = 0;
+ socklen_t len = (socklen_t)sizeof(error);
+
+ getsockopt(sock->fd, SOL_SOCKET, SO_ERROR, (void*)&error, &len);
+ if (error != 0) {
+ if (error == EINPROGRESS
+#if EWOULDBLOCK != EAGAIN
+ || error == EWOULDBLOCK
+#endif
+ || error == EAGAIN) {
+ return 0;
+ }
+ // unrecoverable error, reconnect
+ self->need_reconnect = true;
+ return 0;
+ }
+ self->is_ready = true;
+ if (sock->event) {
+ sock->event(sock, self->conn_event, perf_get_time() - self->conn_ts);
+ self->conn_event = perf_socket_event_reconnected;
+ }
+ if (self->is_sending) {
+ uint16_t dnslen;
+ ssize_t n;
+
+ memcpy(&dnslen, self->sendbuf, 2);
+ dnslen = ntohs(dnslen);
+ n = sendto(sock->fd, self->sendbuf + self->sending, dnslen + 2 - self->sending, 0, 0, 0);
+ if (n < 0) {
+ if (errno != EAGAIN) {
+ self->need_reconnect = true;
+ }
+ return 0;
+ }
+ self->sending += n;
+ if (self->sending < dnslen + 2) {
+ return 0;
+ }
+ self->sending = 0;
+ self->is_sending = false;
+ if (sock->sent) {
+ sock->sent(sock, self->qid);
+ }
+ self->nqpc_sent++;
+ }
+ return 1;
+ }
+ default:
+ break;
+ }
+
+ return -1;
+}
+
+static bool perf__tcp_have_more(struct perf_net_socket* sock)
+{
+ return self->have_more;
+}
+
+static void perf__tcp_num_queries_per_conn(struct perf_net_socket* sock, size_t num_queries_per_conn, size_t timeout)
+{
+ self->num_queries_per_conn = num_queries_per_conn;
+ self->nqpc_timeout = timeout;
+}
+
+struct perf_net_socket* perf_net_tcp_opensocket(const perf_sockaddr_t* server, const perf_sockaddr_t* local, size_t bufsize, void* data, perf_net_sent_cb_t sent, perf_net_event_cb_t event)
+{
+ struct perf__tcp_socket* tmp = calloc(1, sizeof(struct perf__tcp_socket)); // clang scan-build
+ struct perf_net_socket* sock = (struct perf_net_socket*)tmp;
+
+ if (!sock) {
+ perf_log_fatal("perf_net_tcp_opensocket() out of memory");
+ return 0; // needed for clang scan build
+ }
+
+ sock->recv = perf__tcp_recv;
+ sock->sendto = perf__tcp_sendto;
+ sock->close = perf__tcp_close;
+ sock->sockeq = perf__tcp_sockeq;
+ sock->sockready = perf__tcp_sockready;
+ sock->have_more = perf__tcp_have_more;
+
+ sock->num_queries_per_conn = perf__tcp_num_queries_per_conn;
+
+ sock->data = data;
+ sock->sent = sent;
+ sock->event = event;
+
+ self->server = *server;
+ self->local = *local;
+ self->bufsize = bufsize;
+ if (self->bufsize > 0) {
+ self->bufsize *= 1024;
+ }
+ self->conning_event = perf_socket_event_connecting;
+ self->conn_event = perf_socket_event_connected;
+
+ sock->fd = perf__tcp_connect(sock);
+ self->recvfd = sock->fd;
+ self->recv_need_reconn = -1;
+
+ return sock;
+}
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "config.h"
+
+#include "net.h"
+
+#include "log.h"
+#include "strerror.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+#define self ((struct perf__udp_socket*)sock)
+
+struct perf__udp_socket {
+ struct perf_net_socket base;
+};
+
+static ssize_t perf__udp_recv(struct perf_net_socket* sock, void* buf, size_t len, int flags)
+{
+ return recv(sock->fd, buf, len, flags);
+}
+
+static ssize_t perf__udp_sendto(struct perf_net_socket* sock, uint16_t qid, const void* buf, size_t len, int flags, const struct sockaddr* dest_addr, socklen_t addrlen)
+{
+ return sendto(sock->fd, buf, len, flags, dest_addr, addrlen);
+}
+
+static int perf__udp_close(struct perf_net_socket* sock)
+{
+ return close(sock->fd);
+}
+
+static int perf__udp_sockeq(struct perf_net_socket* sock_a, struct perf_net_socket* sock_b)
+{
+ return sock_a->fd == sock_b->fd;
+}
+
+static int perf__udp_sockready(struct perf_net_socket* sock, int pipe_fd, int64_t timeout)
+{
+ return 1;
+}
+
+struct perf_net_socket* perf_net_udp_opensocket(const perf_sockaddr_t* server, const perf_sockaddr_t* local, size_t bufsize, void* data, perf_net_sent_cb_t sent, perf_net_event_cb_t event)
+{
+ struct perf__udp_socket* tmp = calloc(1, sizeof(struct perf__udp_socket)); // clang scan-build
+ struct perf_net_socket* sock = (struct perf_net_socket*)tmp;
+
+ int ret, flags;
+
+ if (!sock) {
+ perf_log_fatal("perf_net_udp_opensocket() out of memory");
+ return 0; // needed for clang scan build
+ }
+
+ sock->recv = perf__udp_recv;
+ sock->sendto = perf__udp_sendto;
+ sock->close = perf__udp_close;
+ sock->sockeq = perf__udp_sockeq;
+ sock->sockready = perf__udp_sockready;
+
+ sock->data = data;
+ sock->sent = sent;
+ sock->event = event;
+
+ sock->fd = socket(server->sa.sa.sa_family, SOCK_DGRAM, 0);
+ if (sock->fd == -1) {
+ char __s[256];
+ perf_log_fatal("socket: %s", perf_strerror_r(errno, __s, sizeof(__s)));
+ }
+
+ if (server->sa.sa.sa_family == AF_INET6) {
+ int on = 1;
+
+ if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) == -1) {
+ perf_log_warning("setsockopt(IPV6_V6ONLY) failed");
+ }
+ }
+
+ if (bind(sock->fd, &local->sa.sa, local->length) == -1) {
+ char __s[256];
+ perf_log_fatal("bind: %s", perf_strerror_r(errno, __s, sizeof(__s)));
+ }
+
+ if (bufsize > 0) {
+ bufsize *= 1024;
+
+ ret = setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
+ &bufsize, sizeof(bufsize));
+ if (ret < 0)
+ perf_log_warning("setsockbuf(SO_RCVBUF) failed");
+
+ ret = setsockopt(sock->fd, SOL_SOCKET, SO_SNDBUF,
+ &bufsize, sizeof(bufsize));
+ if (ret < 0)
+ perf_log_warning("setsockbuf(SO_SNDBUF) failed");
+ }
+
+ flags = fcntl(sock->fd, F_GETFL, 0);
+ if (flags < 0)
+ perf_log_fatal("fcntl(F_GETFL)");
+ ret = fcntl(sock->fd, F_SETFL, flags | O_NONBLOCK);
+ if (ret < 0)
+ perf_log_fatal("fcntl(F_SETFL)");
+
+ return sock;
+}
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "config.h"
+
+#include "opt.h"
+
+#include "log.h"
+#include "util.h"
+#include "result.h"
+
+#include <getopt.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+#include <netinet/in.h>
+
+#define MAX_OPTS 64
+#define LINE_LENGTH 80
+
+typedef struct {
+ char c;
+ perf_opttype_t type;
+ const char* desc;
+ const char* help;
+ const char* defval;
+ char defvalbuf[512];
+ union {
+ void* valp;
+ char** stringp;
+ bool* boolp;
+ unsigned int* uintp;
+ uint64_t* uint64p;
+ double* doublep;
+ in_port_t* portp;
+ } u;
+} opt_t;
+
+typedef struct long_opt long_opt_t;
+struct long_opt {
+ long_opt_t* next;
+ const char* name;
+ perf_opttype_t type;
+ const char* desc;
+ const char* help;
+ const char* defval;
+ char defvalbuf[512];
+ union {
+ void* valp;
+ char** stringp;
+ bool* boolp;
+ unsigned int* uintp;
+ uint64_t* uint64p;
+ double* doublep;
+ in_port_t* portp;
+ } u;
+};
+
+static opt_t opts[MAX_OPTS];
+static long_opt_t* longopts = 0;
+static unsigned int nopts;
+static char optstr[MAX_OPTS * 2 + 2 + 1] = { 0 };
+extern const char* progname;
+
+void perf_opt_add(char c, perf_opttype_t type, const char* desc, const char* help,
+ const char* defval, void* valp)
+{
+ opt_t* opt;
+
+ if (nopts == MAX_OPTS) {
+ perf_log_fatal("too many defined options");
+ return;
+ }
+ opt = &opts[nopts++];
+ opt->c = c;
+ opt->type = type;
+ opt->desc = desc;
+ opt->help = help;
+ if (defval != NULL) {
+ if (strlen(defval) > sizeof(opt->defvalbuf) - 1) {
+ perf_log_fatal("perf_opt_add(): defval too large");
+ return;
+ }
+ strncpy(opt->defvalbuf, defval, sizeof(opt->defvalbuf));
+ opt->defvalbuf[sizeof(opt->defvalbuf) - 1] = 0;
+ opt->defval = opt->defvalbuf;
+ } else {
+ opt->defval = NULL;
+ }
+ opt->u.valp = valp;
+
+ char newoptstr[sizeof(optstr) + 2];
+ snprintf(newoptstr, sizeof(newoptstr), "%s%c%s", optstr, c, (type == perf_opt_boolean ? "" : ":"));
+ memcpy(optstr, newoptstr, sizeof(optstr) - 1);
+ optstr[sizeof(optstr) - 1] = 0;
+}
+
+void perf_long_opt_add(const char* name, perf_opttype_t type, const char* desc, const char* help, const char* defval, void* valp)
+{
+ long_opt_t* opt = calloc(1, sizeof(long_opt_t));
+
+ if (!opt) {
+ perf_log_fatal("perf_long_opt_add(): out of memory");
+ return;
+ }
+
+ opt->name = name;
+ opt->type = type;
+ opt->desc = desc;
+ opt->help = help;
+ if (defval != NULL) {
+ if (strlen(defval) > sizeof(opt->defvalbuf) - 1) {
+ perf_log_fatal("perf_opt_add(): defval too large");
+ free(opt); // fix clang scan-build
+ return;
+ }
+ strncpy(opt->defvalbuf, defval, sizeof(opt->defvalbuf));
+ opt->defvalbuf[sizeof(opt->defvalbuf) - 1] = 0;
+ opt->defval = opt->defvalbuf;
+ } else {
+ opt->defval = NULL;
+ }
+ opt->u.valp = valp;
+
+ opt->next = longopts;
+ longopts = opt;
+}
+
+void perf_opt_usage(void)
+{
+ unsigned int prefix_len, position, arg_len, i, j;
+
+ prefix_len = fprintf(stderr, "Usage: %s", progname);
+ position = prefix_len;
+ for (i = 0; i < nopts; i++) {
+ arg_len = 6;
+ if (opts[i].desc != NULL)
+ arg_len += strlen(opts[i].desc) + 1;
+ if (LINE_LENGTH - position - 1 < arg_len) {
+ fprintf(stderr, "\n");
+ for (j = 0; j < prefix_len; j++)
+ fprintf(stderr, " ");
+ position = prefix_len;
+ }
+ fprintf(stderr, " [-%c", opts[i].c);
+ if (opts[i].desc != NULL)
+ fprintf(stderr, " %s", opts[i].desc);
+ fprintf(stderr, "]");
+ position += arg_len;
+ }
+ fprintf(stderr, "\n");
+
+ for (i = 0; i < nopts; i++) {
+ fprintf(stderr, " -%c %s", opts[i].c, opts[i].help);
+ if (opts[i].defval)
+ fprintf(stderr, " (default: %s)", opts[i].defval);
+ fprintf(stderr, "\n");
+ }
+}
+
+static uint32_t
+parse_uint(const char* desc, const char* str,
+ unsigned int min, unsigned int max)
+{
+ unsigned long int val;
+ uint32_t ret;
+ char* endptr = 0;
+
+ errno = 0;
+ val = strtoul(str, &endptr, 10);
+ if (!errno && str && *str && endptr && !*endptr && val <= UINT32_MAX) {
+ ret = (uint32_t)val;
+ if (ret >= min && ret <= max) {
+ return ret;
+ }
+ }
+
+ fprintf(stderr, "invalid %s: %s\n", desc, str);
+ perf_opt_usage();
+ exit(1);
+}
+
+static double
+parse_double(const char* desc, const char* str)
+{
+ const char* s;
+ char c;
+ bool seen_dot = false;
+
+ s = str;
+ while (*s != 0) {
+ c = *s++;
+ if (c == '.') {
+ if (seen_dot)
+ goto fail;
+ seen_dot = true;
+ } else if (c < '0' || c > '9') {
+ goto fail;
+ }
+ }
+
+ return atof(str);
+
+fail:
+ fprintf(stderr, "invalid %s: %s\n", desc, str);
+ perf_opt_usage();
+ exit(1);
+}
+
+static uint64_t
+parse_timeval(const char* desc, const char* str)
+{
+ return MILLION * parse_double(desc, str);
+}
+
+static int perf_opt_long_parse(char* optarg)
+{
+ ssize_t optlen;
+ char* arg;
+
+ if ((arg = strchr(optarg, '='))) {
+ optlen = arg - optarg;
+ arg++;
+ if (optlen < 1 || !strlen(arg)) {
+ return -1;
+ }
+ } else {
+ optlen = strlen(optarg);
+ }
+
+ long_opt_t* opt = longopts;
+ while (opt) {
+ if (!strncmp(optarg, opt->name, optlen)) {
+ switch (opt->type) {
+ case perf_opt_string:
+ if (!arg) {
+ return -1;
+ }
+ *opt->u.stringp = arg;
+ break;
+ case perf_opt_boolean:
+ *opt->u.boolp = true;
+ break;
+ case perf_opt_uint:
+ if (!arg) {
+ return -1;
+ }
+ *opt->u.uintp = parse_uint(opt->desc, arg, 1, 0xFFFFFFFF);
+ break;
+ case perf_opt_zpint:
+ if (!arg) {
+ return -1;
+ }
+ *opt->u.uintp = parse_uint(opt->desc, arg, 0, 0xFFFFFFFF);
+ break;
+ case perf_opt_timeval:
+ if (!arg) {
+ return -1;
+ }
+ *opt->u.uint64p = parse_timeval(opt->desc, arg);
+ break;
+ case perf_opt_double:
+ if (!arg) {
+ return -1;
+ }
+ *opt->u.doublep = parse_double(opt->desc, arg);
+ break;
+ case perf_opt_port:
+ if (!arg) {
+ return -1;
+ }
+ *opt->u.portp = parse_uint(opt->desc, arg, 0, 0xFFFF);
+ break;
+ }
+ return 0;
+ }
+ opt = opt->next;
+ }
+
+ return -1;
+}
+
+void perf_long_opt_usage(void)
+{
+ fprintf(stderr, "Usage: %s ... -O <name>[=<value>] ...\n\nAvailable long options:\n", progname);
+ long_opt_t* opt = longopts;
+ while (opt) {
+ if (opt->type == perf_opt_boolean) {
+ fprintf(stderr, " %s: %s", opt->name, opt->help);
+ } else {
+ fprintf(stderr, " %s=<%s>: %s", opt->name, opt->desc ? opt->desc : "val", opt->help);
+ }
+ if (opt->defval) {
+ fprintf(stderr, " (default: %s)", opt->defval);
+ }
+ fprintf(stderr, "\n");
+
+ opt = opt->next;
+ }
+}
+
+void perf_opt_parse(int argc, char** argv)
+{
+ int c;
+ opt_t* opt;
+ unsigned int i;
+
+ perf_opt_add('h', perf_opt_boolean, NULL, "print this help", NULL, NULL);
+ perf_opt_add('H', perf_opt_boolean, NULL, "print long options help", NULL, NULL);
+ perf_opt_add('O', perf_opt_string, NULL, "set long options: <name>=<value>", NULL, NULL);
+
+ while ((c = getopt(argc, argv, optstr)) != -1) {
+ for (i = 0; i < nopts; i++) {
+ if (opts[i].c == c)
+ break;
+ }
+ if (i == nopts) {
+ perf_opt_usage();
+ exit(1);
+ }
+ if (c == 'h') {
+ perf_opt_usage();
+ exit(0);
+ }
+ if (c == 'H') {
+ perf_long_opt_usage();
+ exit(0);
+ }
+ if (c == 'O') {
+ if (perf_opt_long_parse(optarg)) {
+ fprintf(stderr, "invalid long option: %s\n", optarg);
+ perf_opt_usage();
+ exit(1);
+ }
+ continue;
+ }
+ opt = &opts[i];
+ switch (opt->type) {
+ case perf_opt_string:
+ *opt->u.stringp = optarg;
+ break;
+ case perf_opt_boolean:
+ *opt->u.boolp = true;
+ break;
+ case perf_opt_uint:
+ *opt->u.uintp = parse_uint(opt->desc, optarg,
+ 1, 0xFFFFFFFF);
+ break;
+ case perf_opt_zpint:
+ *opt->u.uintp = parse_uint(opt->desc, optarg,
+ 0, 0xFFFFFFFF);
+ break;
+ case perf_opt_timeval:
+ *opt->u.uint64p = parse_timeval(opt->desc, optarg);
+ break;
+ case perf_opt_double:
+ *opt->u.doublep = parse_double(opt->desc, optarg);
+ break;
+ case perf_opt_port:
+ *opt->u.portp = parse_uint(opt->desc, optarg,
+ 0, 0xFFFF);
+ break;
+ }
+ }
+ if (optind != argc) {
+ fprintf(stderr, "unexpected argument %s\n", argv[optind]);
+ perf_opt_usage();
+ exit(1);
+ }
+}
+
+perf_suppress_t perf_opt_parse_suppress(const char* val)
+{
+ perf_suppress_t s = { false, false, false, false };
+
+ while (val && *val) {
+ const char* next = strchr(val, ',');
+ int len;
+ if (next) {
+ len = next - val;
+ next += 1;
+ } else {
+ len = strlen(val);
+ next = 0;
+ }
+
+ if (!strncmp(val, "timeouts", len)) {
+ s.timeouts = true;
+ } else if (!strncmp(val, "congestion", len)) {
+ s.congestion = true;
+ } else if (!strncmp(val, "sendfailed", len)) {
+ s.sendfailed = true;
+ } else if (!strncmp(val, "sockready", len)) {
+ s.sockready = true;
+ } else if (!strncmp(val, "unexpected", len)) {
+ s.unexpected = true;
+ } else {
+ fprintf(stderr, "unknown message type to suppress: %.*s\n", len, val);
+ perf_opt_usage();
+ exit(1);
+ }
+
+ val = next;
+ }
+
+ return s;
+}
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef PERF_OPT_H
+#define PERF_OPT_H 1
+
+#include <stdbool.h>
+
+typedef enum {
+ perf_opt_string,
+ perf_opt_boolean,
+ perf_opt_uint, // can not be zero
+ perf_opt_zpint, // zero or positive
+ perf_opt_timeval,
+ perf_opt_double,
+ perf_opt_port,
+} perf_opttype_t;
+
+typedef struct {
+ bool timeouts;
+ bool congestion;
+ bool sendfailed;
+ bool sockready;
+ bool unexpected;
+} perf_suppress_t;
+
+void perf_opt_add(char c, perf_opttype_t type, const char* desc, const char* help, const char* defval, void* valp);
+void perf_long_opt_add(const char* name, perf_opttype_t type, const char* desc, const char* help, const char* defval, void* valp);
+
+void perf_opt_usage(void);
+void perf_long_opt_usage(void);
+
+void perf_opt_parse(int argc, char** argv);
+
+perf_suppress_t perf_opt_parse_suppress(const char* val);
+
+#endif
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "config.h"
+
+#include "os.h"
+
+#include "log.h"
+#include "util.h"
+
+#include <errno.h>
+#include <signal.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <poll.h>
+
+#if defined(HAVE_PTHREAD_NP_H)
+#include <pthread_np.h>
+#endif /* if defined(HAVE_PTHREAD_NP_H) */
+
+void perf_os_blocksignal(int sig, bool block)
+{
+ sigset_t sset;
+ int op;
+
+ op = block ? SIG_BLOCK : SIG_UNBLOCK;
+
+ if (sigemptyset(&sset) < 0 || sigaddset(&sset, sig) < 0 || pthread_sigmask(op, &sset, NULL) < 0) {
+ char __s[256];
+ perf_log_fatal("pthread_sigmask: %s", perf_strerror_r(errno, __s, sizeof(__s)));
+ }
+}
+
+void perf_os_handlesignal(int sig, void (*handler)(int))
+{
+ struct sigaction sa;
+
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_handler = handler;
+
+ if (sigfillset(&sa.sa_mask) < 0 || sigaction(sig, &sa, NULL) < 0) {
+ char __s[256];
+ perf_log_fatal("sigaction: %s", perf_strerror_r(errno, __s, sizeof(__s)));
+ }
+}
+
+perf_result_t
+perf_os_waituntilreadable(struct perf_net_socket* sock, int pipe_fd, int64_t timeout)
+{
+ struct perf_net_socket* socks[] = { sock };
+ return perf_os_waituntilanyreadable(socks, 1, pipe_fd, timeout);
+}
+
+perf_result_t
+perf_os_waituntilanyreadable(struct perf_net_socket** socks, unsigned int nfds, int pipe_fd,
+ int64_t timeout)
+{
+ struct pollfd fds[nfds + 1];
+ size_t i;
+ int to, n;
+
+ for (i = 0; i < nfds; i++) {
+ if (perf_net_have_more(socks[i]))
+ return (PERF_R_SUCCESS);
+
+ fds[i].fd = socks[i]->fd;
+ fds[i].events = POLLIN;
+ }
+
+ fds[nfds].fd = pipe_fd;
+ fds[nfds].events = POLLIN;
+
+ if (timeout < 0) {
+ to = -1;
+ } else {
+ to = timeout / 1000;
+ if (timeout && !to) {
+ to = 1;
+ }
+ }
+
+ n = poll(fds, nfds + 1, to);
+ if (n < 0) {
+ if (errno != EINTR) {
+ char __s[256];
+ perf_log_fatal("select(): %s", perf_strerror_r(errno, __s, sizeof(__s)));
+ }
+ return (PERF_R_CANCELED);
+ } else if (n == 0) {
+ return (PERF_R_TIMEDOUT);
+ } else if (fds[nfds].revents & POLLIN) {
+ return (PERF_R_CANCELED);
+ } else {
+ return (PERF_R_SUCCESS);
+ }
+}
+
+perf_result_t
+perf_os_waituntilanywritable(struct perf_net_socket** socks, unsigned int nfds, int pipe_fd,
+ int64_t timeout)
+{
+ struct pollfd fds[nfds + 1];
+ size_t i;
+ int to, n;
+
+ for (i = 0; i < nfds; i++) {
+ fds[i].fd = socks[i]->fd;
+ fds[i].events = POLLOUT;
+ }
+
+ fds[nfds].fd = pipe_fd;
+ fds[nfds].events = POLLIN;
+
+ if (timeout < 0) {
+ to = -1;
+ } else {
+ to = timeout / 1000;
+ if (timeout && !to) {
+ to = 1;
+ }
+ }
+
+ n = poll(fds, nfds + 1, to);
+ if (n < 0) {
+ if (errno != EINTR) {
+ char __s[256];
+ perf_log_fatal("select(): %s", perf_strerror_r(errno, __s, sizeof(__s)));
+ }
+ return (PERF_R_CANCELED);
+ } else if (n == 0) {
+ return (PERF_R_TIMEDOUT);
+ } else if (fds[nfds].revents & POLLIN) {
+ return (PERF_R_CANCELED);
+ } else {
+ return (PERF_R_SUCCESS);
+ }
+}
+
+void perf_os_thread_setname(pthread_t thread, const char* name)
+{
+#if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__APPLE__)
+ /*
+ * macOS has pthread_setname_np but only works on the
+ * current thread so it's not used here
+ */
+#if defined(__NetBSD__)
+ (void)pthread_setname_np(thread, name, NULL);
+#else /* if defined(__NetBSD__) */
+ (void)pthread_setname_np(thread, name);
+#endif /* if defined(__NetBSD__) */
+#elif defined(HAVE_PTHREAD_SET_NAME_NP)
+ (void)pthread_set_name_np(thread, name);
+#else /* if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__APPLE__) */
+ (void)(thread);
+ (void)(name);
+#endif /* if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__APPLE__) */
+}
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "net.h"
+#include "result.h"
+
+#ifndef PERF_OS_H
+#define PERF_OS_H 1
+
+#include <inttypes.h>
+#include <stdbool.h>
+#include <pthread.h>
+
+void perf_os_blocksignal(int sig, bool block);
+
+void perf_os_handlesignal(int sig, void (*handler)(int));
+
+perf_result_t
+perf_os_waituntilreadable(struct perf_net_socket* sock, int pipe_fd, int64_t timeout);
+
+perf_result_t
+perf_os_waituntilanyreadable(struct perf_net_socket** socks, unsigned int nfds, int pipe_fd,
+ int64_t timeout);
+
+perf_result_t
+perf_os_waituntilanywritable(struct perf_net_socket** socks, unsigned int nfds, int pipe_fd,
+ int64_t timeout);
+
+void perf_os_thread_setname(pthread_t thread, const char* name);
+
+#endif
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "config.h"
+
+#include "qtype.h"
+
+const perf_qtype_t qtype_table[] = {
+ { "A", 1 },
+ { "NS", 2 },
+ { "MD", 3 },
+ { "MF", 4 },
+ { "CNAME", 5 },
+ { "SOA", 6 },
+ { "MB", 7 },
+ { "MG", 8 },
+ { "MR", 9 },
+ { "NULL", 10 },
+ { "WKS", 11 },
+ { "PTR", 12 },
+ { "HINFO", 13 },
+ { "MINFO", 14 },
+ { "MX", 15 },
+ { "TXT", 16 },
+ { "RP", 17 },
+ { "AFSDB", 18 },
+ { "X25", 19 },
+ { "ISDN", 20 },
+ { "RT", 21 },
+ { "NSAP", 22 },
+ { "NSAP-PTR", 23 },
+ { "SIG", 24 },
+ { "KEY", 25 },
+ { "PX", 26 },
+ { "GPOS", 27 },
+ { "AAAA", 28 },
+ { "LOC", 29 },
+ { "NXT", 30 },
+ { "EID", 31 },
+ { "NIMLOC", 32 },
+ { "SRV", 33 },
+ { "ATMA", 34 },
+ { "NAPTR", 35 },
+ { "KX", 36 },
+ { "CERT", 37 },
+ { "A6", 38 },
+ { "DNAME", 39 },
+ { "SINK", 40 },
+ { "OPT", 41 },
+ { "APL", 42 },
+ { "DS", 43 },
+ { "SSHFP", 44 },
+ { "IPSECKEY", 45 },
+ { "RRSIG", 46 },
+ { "NSEC", 47 },
+ { "DNSKEY", 48 },
+ { "DHCID", 49 },
+ { "NSEC3", 50 },
+ { "NSEC3PARAM", 51 },
+ { "TLSA", 52 },
+ { "SMIMEA", 53 },
+ { "HIP", 55 },
+ { "NINFO", 56 },
+ { "RKEY", 57 },
+ { "TALINK", 58 },
+ { "CDS", 59 },
+ { "CDNSKEY", 60 },
+ { "OPENPGPKEY", 61 },
+ { "CSYNC", 62 },
+ { "ZONEMD", 63 },
+ { "SVCB", 64 },
+ { "HTTPS", 65 },
+ { "SPF", 99 },
+ { "UINFO", 100 },
+ { "UID", 101 },
+ { "GID", 102 },
+ { "UNSPEC", 103 },
+ { "NID", 104 },
+ { "L32", 105 },
+ { "L64", 106 },
+ { "LP", 107 },
+ { "EUI48", 108 },
+ { "EUI64", 109 },
+ { "TKEY", 249 },
+ { "TSIG", 250 },
+ { "IXFR", 251 },
+ { "AXFR", 252 },
+ { "MAILB", 253 },
+ { "MAILA", 254 },
+ { "ANY", 255 },
+ { "URI", 256 },
+ { "CAA", 257 },
+ { "AVC", 258 },
+ { "DOA", 259 },
+ { "AMTRELAY", 260 },
+ { "TA", 32768 },
+ { "DLV", 32769 },
+ { 0, 0 }
+};
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef PERF_QTYPE_H
+#define PERF_QTYPE_H 1
+
+#include <stdint.h>
+
+typedef struct perf_qtype {
+ char* type;
+ uint16_t value;
+} perf_qtype_t;
+
+extern const perf_qtype_t qtype_table[];
+
+#endif
--- /dev/null
+#!/bin/sh
+#
+# Copyright 2019-2026 OARC, Inc.
+# Copyright 2017-2018 Akamai Technologies
+# Copyright 2006-2016 Nominum, Inc.
+# All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# Driver script to run resperf and generate an HTML report of
+# the results, with graphs.
+#
+
+# Program locations - change these if not in $PATH
+resperf=resperf
+gnuplot=gnuplot
+
+# The gnuplot terminal type. This determines the image format for the
+# plots; "png" or "gif" will both work as long as the corresponding
+# terminal support is compiled into your copy of gnuplot.
+terminal=png
+
+# Create a unique ID for this report
+id=`date '+%Y%m%d-%H%M'`
+
+# Set up file names
+reportfile="$id.html"
+outputfile="$id.output"
+plotfile="$id.gnuplot"
+rate_graph="$id.rate.$terminal"
+latency_graph="$id.latency.$terminal"
+
+# Run the test
+$resperf -P "$plotfile" "$@" >"$outputfile" 2>&1 ||
+ { echo "`basename $0`: error running resperf:" >&2;
+ cat $outputfile >&2;
+ exit 1;
+ }
+
+# Create plots
+
+if
+ $gnuplot <<EOF
+set terminal $terminal
+set output "$rate_graph"
+set title "Query / response / failure rate"
+set key top left
+set xlabel "Time (seconds)"
+set yrange [0:]
+plot \
+"$plotfile" using 1:3 title "Queries sent per second" with lines, \
+"$plotfile" using 1:4 title "Total responses received per second" with lines, \
+"$plotfile" using 1:5 title "Failure responses received per second" with lines
+EOF
+then
+ :
+else
+ echo "`basename $0`: error running gnuplot" >&2; exit 1;
+fi
+
+if
+ $gnuplot <<EOF
+set terminal $terminal
+set output "$latency_graph"
+set title "Latency"
+set key top left
+set xlabel "Time (seconds)"
+set yrange [0:]
+plot \
+"$plotfile" using 1:6 title "Average latency (seconds)" with lines
+EOF
+then
+ :
+else
+ echo "`basename $0`: error running gnuplot" >&2; exit 1;
+fi
+
+# Generate the report
+
+exec >"$reportfile"
+
+cat <<EOF
+<html><head></head><body>
+<h1>Resperf report $id</h1>
+<h2>Resperf output</h2>
+<pre>
+EOF
+cat "$outputfile"
+cat <<EOF
+</pre>
+EOF
+
+cat <<EOF
+<h2>Plots</h2>
+<p>
+<img src="$rate_graph" />
+<img src="$latency_graph" />
+</p>
+</body></html>
+EOF
+
+echo "Done, report is in $reportfile" >&2
--- /dev/null
+.\" Copyright 2019-2026 OARC, Inc.
+.\" Copyright 2017-2018 Akamai Technologies
+.\" Copyright 2006-2016 Nominum, Inc.
+.\" All rights reserved.
+.\"
+.\" Licensed under the Apache License, Version 2.0 (the "License");
+.\" you may not use this file except in compliance with the License.
+.\" You may obtain a copy of the License at
+.\"
+.\" http://www.apache.org/licenses/LICENSE-2.0
+.\"
+.\" Unless required by applicable law or agreed to in writing, software
+.\" distributed under the License is distributed on an "AS IS" BASIS,
+.\" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+.\" See the License for the specific language governing permissions and
+.\" limitations under the License.
+.TH resperf 1 "@PACKAGE_VERSION@" "resperf"
+.SH NAME
+resperf \- test the resolution performance of a caching DNS server
+.SH SYNOPSIS
+.hy 0
+.ad l
+\fBresperf\-report\fR\ [\fB\-a\ \fIlocal_addr\fR]
+[\fB\-d\ \fIdatafile\fR]
+[\fB\-R\fR]
+[\fB\-M\ \fImode\fR]
+[\fB\-s\ \fIserver_addr\fR]
+[\fB\-p\ \fIport\fR]
+[\fB\-x\ \fIlocal_port\fR]
+[\fB\-t\ \fItimeout\fR]
+[\fB\-b\ \fIbufsize\fR]
+[\fB\-f\ \fIfamily\fR]
+[\fB\-e\fR]
+[\fB\-D\fR]
+[\fB\-y\ \fI[alg:]name:secret\fR]
+[\fB\-h\fR]
+[\fB\-i\ \fIinterval\fR]
+[\fB\-m\ \fImax_qps\fR]
+[\fB\-r\ \fIrampup_time\fR]
+[\fB\-c\ \fIconstant_traffic_time\fR]
+[\fB\-L\ \fImax_loss\fR]
+[\fB\-C\ \fIclients\fR]
+[\fB\-q\ \fImax_outstanding\fR]
+[\fB\-F\ \fIfall_behind\fR]
+[\fB\-v\fR]
+[\fB\-W\fR]
+[\fB\-O\ \fIoption=value\fR]
+.ad
+.hy
+.hy 0
+.ad l
+
+\fBresperf\fR\ [\fB\-a\ \fIlocal_addr\fR]
+[\fB\-d\ \fIdatafile\fR]
+[\fB\-R\fR]
+[\fB\-M\ \fImode\fR]
+[\fB\-s\ \fIserver_addr\fR]
+[\fB\-p\ \fIport\fR]
+[\fB\-x\ \fIlocal_port\fR]
+[\fB\-t\ \fItimeout\fR]
+[\fB\-b\ \fIbufsize\fR]
+[\fB\-f\ \fIfamily\fR]
+[\fB\-e\fR]
+[\fB\-D\fR]
+[\fB\-y\ \fI[alg:]name:secret\fR]
+[\fB\-h\fR]
+[\fB\-i\ \fIinterval\fR]
+[\fB\-m\ \fImax_qps\fR]
+[\fB\-P\ \fIplot_data_file\fR]
+[\fB\-r\ \fIrampup_time\fR]
+[\fB\-c\ \fIconstant_traffic_time\fR]
+[\fB\-L\ \fImax_loss\fR]
+[\fB\-C\ \fIclients\fR]
+[\fB\-q\ \fImax_outstanding\fR]
+[\fB\-F\ \fIfall_behind\fR]
+[\fB\-v\fR]
+[\fB\-W\fR]
+[\fB\-O\ \fIoption=value\fR]
+.ad
+.hy
+.SH DESCRIPTION
+\fBresperf\fR is a companion tool to \fBdnsperf\fR.
+\fBdnsperf\fR was primarily designed for benchmarking authoritative
+servers, and it does not work well with caching servers that are talking
+to the live Internet.
+One reason for this is that dnsperf uses a "self-pacing" approach, which is
+based on the assumption that you can keep the server 100% busy simply by
+sending it a small burst of back-to-back queries to fill up network buffers,
+and then send a new query whenever you get a response back.
+This approach works well for authoritative servers that process queries in
+order and one at a time; it also works pretty well for a caching server in
+a closed laboratory environment talking to a simulated Internet that's all
+on the same LAN.
+Unfortunately, it does not work well with a caching server talking
+to the actual Internet, which may need to work on thousands of queries in
+parallel to achieve its maximum throughput.
+There have been numerous attempts to use dnsperf (or its predecessor,
+queryperf) for benchmarking live caching servers, usually with poor results.
+Therefore, a separate tool designed specifically for caching servers is
+needed.
+.SS "How resperf works"
+Unlike the "self-pacing" approach of dnsperf, \fBresperf\fR works by sending
+DNS queries at a controlled, steadily increasing rate.
+By default, \fBresperf\fR will send traffic for 60 seconds, linearly
+increasing the amount of traffic from zero to 100,000 queries per second (or
+\fImax_qps\fR).
+
+During the test, \fBresperf\fR listens for responses from the server and
+keeps track of response rates, failure rates, and latencies.
+It will also continue listening for responses for an additional 40 seconds
+after it has stopped sending traffic, so that there is time for the server
+to respond to the last queries sent.
+This time period was chosen to be longer than the overall query timeout of
+both Nominum CacheServe and current versions of BIND.
+
+If the test is successful, the query rate will at some point exceed the
+capacity of the server and queries will be dropped, causing the response
+rate to stop growing or even decrease as the query rate increases.
+
+The result of the test is a set of measurements of the query rate, response
+rate, failure response rate, and average query latency as functions of time.
+.SS "What you will need"
+Benchmarking a live caching server is serious business.
+A fast caching server like Nominum CacheServe, resolving a mix of cacheable
+and non-cacheable queries typical of ISP customer traffic, is capable of
+resolving well over 1,000,000 queries per second.
+In the process, it will send more than 40,000 queries per second to
+authoritative servers on the Internet, and receive responses to most of them.
+Assuming an average request size of 50 bytes and a response size of 150
+bytes, this amounts to some 1216 Mbps of outgoing and 448 Mbps of incoming
+traffic.
+If your Internet connection can't handle the bandwidth, you will end up
+measuring the speed of the connection, not the server, and may saturate the
+connection causing a degradation in service for other users.
+
+Make sure there is no stateful firewall between the server and the Internet,
+because most of them can't handle the amount of UDP traffic the test will
+generate and will end up dropping packets, skewing the test results.
+Some will even lock up or crash.
+
+You should run \fBresperf\fR on a machine separate from the server under test,
+on the same LAN.
+Preferably, this should be a Gigabit Ethernet network.
+The machine running \fBresperf\fR should be at least as fast as the machine
+being tested; otherwise, it may end up being the bottleneck.
+
+There should be no other applications running on the machine running
+\fBresperf\fR.
+Performance testing at the traffic levels involved is essentially a
+hard real-time application - consider the fact that at a query rate of
+100,000 queries per second, if \fBresperf\fR gets delayed by just 1/100 of a
+second, 1000 incoming UDP packets will arrive in the meantime.
+This is more than most operating systems will buffer, which means packets
+will be dropped.
+
+Because the granularity of the timers provided by operating systems is
+typically too coarse to accurately schedule packet transmissions at
+sub-millisecond intervals, \fBresperf\fR will busy-wait between packet
+transmissions, constantly polling for responses in the meantime.
+Therefore, it is normal for \fBresperf\fR to consume 100% CPU during the
+whole test run, even during periods where query rates are relatively low.
+
+You will also need a set of test queries in the \fBdnsperf\fR file format.
+See the \fBdnsperf\fR man page for instructions on how to construct this
+query file.
+To make the test as realistic as possible, the queries should be derived
+from recorded production client DNS traffic, without removing duplicate
+queries or other filtering.
+With the default settings, \fBresperf\fR will use up to 3 million queries
+in each test run.
+
+If the caching server to be tested has a configurable limit on the number of
+simultaneous resolutions, like the \fBmax\-recursive\-clients\fR statement
+in Nominum CacheServe or the \fBrecursive\-clients\fR option in BIND 9, you
+will probably have to increase it.
+As a starting point, we recommend a value of 10000 for Nominum CacheServe
+and 100000 for BIND 9.
+Should the limit be reached, it will show up in the plots as an increase in
+the number of failure responses.
+
+The server being tested should be restarted at the beginning of each test to
+make sure it is starting with an empty cache.
+If the cache already contains data from a previous test run that used the
+same set of queries, almost all queries will be answered from the cache,
+yielding inflated performance numbers.
+
+To use the \fBresperf\-report\fR script, you need to have \fBgnuplot\fR
+installed.
+Make sure your installed version of \fBgnuplot\fR supports the png terminal
+driver.
+If your \fBgnuplot\fR doesn't support png but does support gif, you can
+change the line saying terminal=png in the \fBresperf\-report\fR script
+to terminal=gif.
+.SS "Running the test"
+\fBresperf\fR is typically invoked via the \fBresperf\-report\fR script,
+which will run \fBresperf\fR with its output redirected to a file and then
+automatically generate an illustrated report in HTML format.
+Command line arguments given to \fBresperf\-report\fR will be passed on
+unchanged to \fBresperf\fR.
+
+When running \fBresperf\-report\fR, you will need to specify at least the
+server IP address and the query data file.
+A typical invocation will look like
+.RS
+.hy 0
+
+.nf
+resperf\-report \-s 10.0.0.2 \-d queryfile
+.fi
+.hy
+.RE
+
+With default settings, the test run will take at most 100 seconds (60
+seconds of ramping up traffic and then 40 seconds of waiting for responses),
+but in practice, the 60-second traffic phase will usually be cut short.
+To be precise, \fBresperf\fR can transition from the traffic-sending phase
+to the waiting-for-responses phase in three different ways:
+.IP \(bu 2
+Running for the full allotted time and successfully reaching the maximum
+query rate (by default, 60 seconds and 100,000 qps, respectively).
+Since this is a very high query rate, this will rarely happen (with today's
+hardware); one of the other two conditions listed below will usually occur
+first.
+.IP \(bu 2
+Exceeding 65,536 outstanding queries.
+This often happens as a result of (successfully) exceeding the capacity of
+the server being tested, causing the excess queries to be dropped.
+The limit of 65,536 queries comes from the number of possible values for
+the ID field in the DNS packet.
+\fBresperf\fR needs to allocate a unique ID for each outstanding query, and is
+therefore unable to send further queries if the set of possible IDs is
+exhausted.
+.IP \(bu 2
+When \fBresperf\fR finds itself unable to send queries fast enough.
+\fBresperf\fR will notice if it is falling behind in its scheduled query
+transmissions, and if this backlog reaches 1000 queries, it will print
+a message like "Fell behind by 1000 queries" (or whatever the actual number
+is at the time) and stop sending traffic.
+.PP
+Regardless of which of the above conditions caused the traffic-sending phase
+of the test to end, you should examine the resulting plots to make sure the
+server's response rate is flattening out toward the end of the test.
+If it is not, then you are not loading the server enough.
+If you are getting the "Fell behind" message, make sure that the machine
+running \fBresperf\fR is fast enough and has no other applications running.
+
+You should also monitor the CPU usage of the server under test.
+It should reach close to 100% CPU at the point of maximum traffic; if it does
+not, you most likely have a bottleneck in some other part of your test setup,
+for example, your external Internet connection.
+
+The report generated by \fBresperf\-report\fR will be stored with a unique
+file name based on the current date and time, e.g.,
+\fI20060812-1550.html\fR.
+The PNG images of the plots and other auxiliary files will be stored in
+separate files beginning with the same date-time string.
+To view the report, simply open the \fI.html\fR file in a web browser.
+
+If you need to copy the report to a separate machine for viewing, make sure
+to copy the .png files along with the .html file (or simply copy all the
+files, e.g., using scp 20060812-1550.* host:directory/).
+.SS "Interpreting the report"
+The \fI.html\fR file produced by \fBresperf\-report\fR consists of two
+sections.
+The first section, "Resperf output", contains output from the \fBresperf\fR
+program such as progress messages, a summary of the command line arguments,
+and summary statistics.
+The second section, "Plots", contains two plots generated by \fBgnuplot\fR:
+"Query/response/failure rate" and "Latency".
+
+The "Query/response/failure rate" plot contains three graphs.
+The "Queries sent per second" graph shows the amount of traffic being sent to
+the server; this should be very close to a straight diagonal line, reflecting
+the linear ramp-up of traffic.
+
+The "Total responses received per second" graph shows how many of the
+queries received a response from the server.
+All responses are counted, whether successful (NOERROR or NXDOMAIN) or not
+(e.g., SERVFAIL).
+
+The "Failure responses received per second" graph shows how many of the
+queries received a failure response.
+A response is considered to be a failure if its RCODE is neither NOERROR
+nor NXDOMAIN.
+
+By visually inspecting the graphs, you can get an idea of how the server
+behaves under increasing load.
+The "Total responses received per second" graph will initially closely
+follow the "Queries sent per second" graph (often rendering it invisible in
+the plot as the two graphs are plotted on top of one another), but when the
+load exceeds the server's capacity, the "Total responses received per second"
+graph may diverge from the "Queries sent per second" graph and flatten out,
+indicating that some of the queries are being dropped.
+
+The "Failure responses received per second" graph will normally show a
+roughly linear ramp close to the bottom of the plot with some random
+fluctuation, since typical query traffic will contain some small percentage
+of failing queries randomly interspersed with the successful ones.
+As the total traffic increases, the number of failures will increase
+proportionally.
+
+If the "Failure responses received per second" graph turns sharply upwards,
+this can be another indication that the load has exceeded the server's
+capacity.
+This will happen if the server reacts to overload by sending SERVFAIL
+responses rather than by dropping queries.
+Since Nominum CacheServe and BIND 9 will both respond with SERVFAIL when
+they exceed their \fBmax\-recursive\-clients\fR or \fBrecursive\-clients\fR
+limit, respectively, a sudden increase in the number of failures could mean
+that the limit needs to be increased.
+
+The "Latency" plot contains a single graph marked "Average latency".
+This shows how the latency varies during the course of the test.
+Typically, the latency graph will exhibit a downwards trend because the
+cache hit rate improves as ever more responses are cached during the test,
+and the latency for a cache hit is much smaller than for a cache miss.
+The latency graph is provided as an aid in determining the point where the
+server gets overloaded, which can be seen as a sharp upwards turn in the
+graph.
+The latency graph is not intended for making absolute latency measurements
+or comparisons between servers; the latencies shown in the graph are not
+representative of production latencies due to the initially empty cache and
+the deliberate overloading of the server towards the end of the test.
+
+Note that all measurements are displayed on the plot at the horizontal
+position corresponding to the point in time when the query was sent, not
+when the response (if any) was received.
+This makes it it easy to compare the query and response rates; for example,
+if no queries are dropped, the query and response graphs will be identical.
+As another example, if the plot shows 10% failure responses at t=5 seconds,
+this means that 10% of the queries sent at t=5 seconds eventually failed,
+not that 10% of the responses received at t=5 seconds were failures.
+.SS "Determining the server's maximum throughput"
+Often, the goal of running \fBresperf\fR is to determine the server's
+maximum throughput, in other words, the number of queries per second it is
+capable of handling.
+This is not always an easy task, because as a server is driven into overload,
+the service it provides may deteriorate gradually, and this deterioration
+can manifest itself either as queries being dropped, as an increase in the
+number of SERVFAIL responses, or an increase in latency.
+The maximum throughput may be defined as the highest level of traffic at
+which the server still provides an acceptable level of service, but that
+means you first need to decide what an acceptable level of service means in
+terms of packet drop percentage, SERVFAIL percentage, and latency.
+
+The summary statistics in the "Resperf output" section of the report
+contains a "Maximum throughput" value which by default is determined from
+the maximum rate at which the server was able to return responses, without
+regard to the number of queries being dropped or failing at that point.
+This method of throughput measurement has the advantage of simplicity, but
+it may or may not be appropriate for your needs; the reported value should
+always be validated by a visual inspection of the graphs to ensure that
+service has not already deteriorated unacceptably before the maximum response
+rate is reached.
+It may also be helpful to look at the "Lost at that point" value in
+the summary statistics; this indicates the percentage of the queries that
+was being dropped at the point in the test when the maximum throughput was
+reached.
+
+Alternatively, you can make \fBresperf\fR report the throughput at the point
+in the test where the percentage of queries dropped exceeds a given limit
+(or the maximum as above if the limit is never exceeded).
+This can be a more realistic indication of how much the server can be loaded
+while still providing an acceptable level of service.
+This is done using the \fB\-L\fR command line option; for example, specifying
+\fB\-L 10\fR makes \fBresperf\fR
+report the highest throughput reached before the server starts dropping more
+than 10% of the queries.
+
+There is no corresponding way of automatically constraining results based on
+the number of failed queries, because unlike dropped queries, resolution
+failures will occur even when the the server is not overloaded, and the
+number of such failures is heavily dependent on the query data and network
+conditions.
+Therefore, the plots should be manually inspected to ensure that there is not
+an abnormal number of failures.
+.SH "GENERATING CONSTANT TRAFFIC"
+In addition to ramping up traffic linearly, \fBresperf\fR also has the
+capability to send a constant stream of traffic.
+This can be useful when using \fBresperf\fR for tasks other than performance
+measurement; for example, it can be used to "soak test" a server by
+subjecting it to a sustained load for an extended period of time.
+
+To generate a constant traffic load, use the \fB\-c\fR command line option,
+together with the \fB\-m\fR option which specifies the desired constant
+query rate.
+For example, to send 10000 queries per second for an hour, use \fB\-m 10000
+\-c 3600\fR.
+This will include the usual 30-second gradual ramp-up of traffic at the
+beginning, which may be useful to avoid initially overwhelming a server that
+is starting with an empty cache.
+To start the onslaught of traffic instantly, use \fB\-m 10000 \-c 3600
+\-r 0\fR.
+
+To be precise, \fBresperf\fR will do a linear ramp-up of traffic from 0 to
+\fB\-m\fR queries per second over a period of \fB\-r\fR seconds, followed by
+a plateau of steady traffic at \fB\-m\fR queries per second lasting for
+\fB\-c\fR seconds, followed by waiting for responses for an extra 40
+seconds.
+Either the ramp-up or the plateau can be suppressed by supplying a duration
+of zero seconds with \fB\-r 0\fR and \fB\-c 0\fR, respectively.
+The latter is the default.
+
+Sending traffic at high rates for hours on end will of course require very
+large amounts of input data.
+Also, a long-running test will generate a large amount of plot data, which is
+kept in memory for the duration of the test.
+To reduce the memory usage and the size of the plot file, consider
+increasing the interval between measurements from the default of 0.5 seconds
+using the \fB\-i\fR option in long-running tests.
+
+When using \fBresperf\fR for long-running tests, it is important that the
+traffic rate specified using the \fB\-m\fR is one that both \fBresperf\fR
+itself and the server under test can sustain.
+Otherwise, the test is likely to be cut short as a result of either running
+out of query IDs (because of large numbers of dropped queries) or of
+\fBresperf\fR falling behind its transmission schedule.
+.SS "Using DNS-over-HTTPS"
+When using DNS-over-HTTPS you must set the \fB-O doh\-uri=...\fR to something
+that works with the server you're sending to.
+Also note that the value for maximum outstanding queries will be used to
+control the maximum concurrent streams within the HTTP/2 connection.
+.SH OPTIONS
+Because the \fBresperf\-report\fR script passes its command line options
+directly to the \fBresperf\fR programs, they both accept the same set of
+options, with one exception: \fBresperf\-report\fR automatically adds an
+appropriate \fB\-P\fR to the \fBresperf\fR command line, and therefore does
+not itself take a \fB\-P\fR option.
+
+\fB-d \fIdatafile\fR
+.br
+.RS
+Specifies the input data file.
+If not specified, \fBresperf\fR will read from standard input.
+.RE
+
+\fB-R\fR
+.br
+.RS
+Reopen the datafile if it runs out of data before the testing is completed.
+This allows for long running tests on very small and simple query datafile.
+.RE
+
+\fB-M \fImode\fR
+.br
+.RS
+Specifies the transport mode to use, "udp", "tcp", "dot" or "doh".
+Default is "udp".
+.RE
+
+\fB-s \fIserver_addr\fR
+.br
+.RS
+Specifies the name or address of the server to which requests will be sent.
+The default is the loopback address, 127.0.0.1.
+.RE
+
+\fB-p \fIport\fR
+.br
+.RS
+Sets the port on which the DNS packets are sent.
+If not specified, the standard DNS port (udp/tcp 53, DoT 853, DoH 443) is used.
+.RE
+
+\fB-a \fIlocal_addr\fR
+.br
+.RS
+Specifies the local address from which to send requests.
+The default is the wildcard address.
+.RE
+
+\fB-x \fIlocal_port\fR
+.br
+.RS
+Specifies the local port from which to send requests.
+The default is the wildcard port (0).
+
+If acting as multiple clients and the wildcard port is used, each client
+will use a different random port.
+If a port is specified, the clients will use a range of ports starting
+with the specified one.
+.RE
+
+\fB-t \fItimeout\fR
+.br
+.RS
+Specifies the request timeout value, in seconds.
+\fBresperf\fR will no longer wait for a response to a particular request
+after this many seconds have elapsed.
+The default is 45 seconds.
+
+\fBresperf\fR times out unanswered requests in order to reclaim query IDs so
+that the query ID space will not be exhausted in a long-running test, such
+as when "soak testing" a server for an day with \fB\-m 10000 \-c 86400\fR.
+The timeouts and the ability to tune them are of little use in the more
+typical use case of a performance test lasting only a minute or two.
+
+The default timeout of 45 seconds was chosen to be longer than the query
+timeout of current caching servers.
+Note that this is longer than the corresponding default in \fBdnsperf\fR,
+because caching servers can take many orders of magnitude longer to answer
+a query than authoritative servers
+do.
+
+If a short timeout is used, there is a possibility that \fBresperf\fR will
+receive a response after the corresponding request has timed out; in this
+case, a message like Warning: Received a response with an unexpected id: 141
+will be printed.
+.RE
+
+\fB-b \fIbufsize\fR
+.br
+.RS
+Sets the size of the socket's send and receive buffers, in kilobytes.
+If not specified, the operating system's default is used.
+.RE
+
+\fB-f \fIfamily\fR
+.br
+.RS
+Specifies the address family used for sending DNS packets.
+The possible values are "inet", "inet6", or "any".
+If "any" (the default value) is specified, \fBresperf\fR will use whichever
+address family is appropriate for the server it is sending packets to.
+.RE
+
+\fB-e\fR
+.br
+.RS
+Enables EDNS0 [RFC2671], by adding an OPT record to all packets sent.
+.RE
+
+\fB-D\fR
+.br
+.RS
+Sets the DO (DNSSEC OK) bit [RFC3225] in all packets sent.
+This also enables EDNS0, which is required for DNSSEC.
+.RE
+
+\fB-y \fI[alg:]name:secret\fR
+.br
+.RS
+Add a TSIG record [RFC2845] to all packets sent, using the specified TSIG
+key algorithm, name and secret, where the algorithm defaults to hmac-md5 and
+the secret is expressed as a base-64 encoded string.
+.RE
+
+\fB-h\fR
+.br
+.RS
+Print a usage statement and exit.
+.RE
+
+\fB-i \fIinterval\fR
+.br
+.RS
+Specifies the time interval between data points in the plot file.
+The default is 0.5 seconds.
+.RE
+
+\fB-m \fImax_qps\fR
+.br
+.RS
+Specifies the target maximum query rate (in queries per second).
+This should be higher than the expected maximum throughput of the server
+being tested.
+Traffic will be ramped up at a linearly increasing rate until this value is
+reached, or until one of the other conditions described in the section
+"Running the test" occurs.
+The default is 100000 queries per second.
+.RE
+
+\fB-P \fIplot_data_file\fR
+.br
+.RS
+Specifies the name of the plot data file.
+The default is \fIresperf.gnuplot\fR.
+.RE
+
+\fB-r \fIrampup_time\fR
+.br
+.RS
+Specifies the length of time over which traffic will be ramped up.
+The default is 60 seconds.
+.RE
+
+\fB-c \fIconstant_traffic_time\fR
+.br
+.RS
+Specifies the length of time for which traffic will be sent at a constant
+rate following the initial ramp-up.
+The default is 0 seconds, meaning no sending of traffic at a constant rate
+will be done.
+.RE
+
+\fB-L \fImax_loss\fR
+.br
+.RS
+Specifies the maximum acceptable query loss percentage for purposes of
+determining the maximum throughput value.
+The default is 100%, meaning that \fBresperf\fR will measure the maximum
+throughput without regard to query
+loss.
+.RE
+
+\fB-C \fIclients\fR
+.br
+.RS
+Act as multiple clients.
+Requests are sent from multiple sockets.
+The default is to act as 1 client.
+.RE
+
+\fB-q \fImax_outstanding\fR
+.br
+.RS
+Sets the maximum number of outstanding requests.
+\fBresperf\fR will stop ramping up traffic when this many queries are
+outstanding.
+The default is 64k, and the limit is 64k per client.
+.RE
+
+\fB-F \fIfall_behind\fR
+.br
+.RS
+Sets the maximum number of queries that can fall behind being sent.
+\fBresperf\fR will stop when this many queries should have been sent and it
+can be relative easy to hit if \fImax_qps\fR is set too high.
+The default is 1000 and setting it to zero (0) disables the check.
+.RE
+
+\fB-v\fR
+.br
+.RS
+Enables verbose mode to report about network readiness and congestion.
+.RE
+
+\fB-W\fR
+.br
+.RS
+Log warnings and errors to standard output instead of standard error making
+it easier for script, test and automation to capture all output.
+.RE
+
+\fB-O \fIoption=value\fR
+.br
+.RS
+Set an extended long option for various things to control different aspects
+of testing or protocol modules, see EXTENDED OPTIONS in \fBdnsperf\fR(1) for
+list of available options.
+.RE
+.SH "THE PLOT DATA FILE"
+The plot data file is written by the \fBresperf\fR program and contains the
+data to be plotted using \fBgnuplot\fR.
+When running \fBresperf\fR via the \fBresperf\-report\fR script, there is
+no need for the user to deal with this file directly, but its format and
+contents are documented here for completeness and in case you wish to run
+\fBresperf\fR directly and use its output for purposes other than viewing
+it with \fBgnuplot\fR.
+
+The first line of the file is a comment identifying the fields.
+It may be recognized as a comment by its leading hash sign (#).
+
+Subsequent lines contain the actual plot data.
+For purposes of generating the plot data file, the test run is divided into
+time intervals of 0.5 seconds (or some other length of time specified with
+the \fB\-i\fR command line option).
+Each line corresponds to one such interval, and contains the following values
+as floating-point numbers:
+
+\fBTime\fR
+.br
+.RS
+The midpoint of this time interval, in seconds since the beginning of the
+run
+.RE
+
+\fBTarget queries per second\fR
+.br
+.RS
+The number of queries per second scheduled to be sent in this time interval
+.RE
+
+\fBActual queries per second\fR
+.br
+.RS
+The number of queries per second actually sent in this time interval
+.RE
+
+\fBResponses per second\fR
+.br
+.RS
+The number of responses received corresponding to queries sent in this time
+interval, divided by the length of the interval
+.RE
+
+\fBFailures per second\fR
+.br
+.RS
+The number of responses received corresponding to queries sent in this time
+interval and having an RCODE other than NOERROR or NXDOMAIN, divided by the
+length of the interval
+.RE
+
+\fBAverage latency\fR
+.br
+.RS
+The average time between sending the query and receiving a response, for
+queries sent in this time interval
+.RE
+
+\fBConnections\fR
+.br
+.RS
+The number of connections done, including re-connections, during this time
+interval.
+This is only relevant to connection oriented protocols, such as TCP and DoT.
+.RE
+
+\fBAverage connection latency\fR
+.br
+.RS
+The average time between starting to connect and having the connection ready
+for sending queries to, for this time interval.
+This is only relevant to connection oriented protocols, such as TCP and DoT.
+.RE
+
+.SH "SEE ALSO"
+\fBdnsperf\fR(1)
+.SH AUTHOR
+Nominum, Inc.
+.LP
+Maintained by DNS-OARC
+.LP
+.RS
+.I https://www.dns-oarc.net/
+.RE
+.LP
+.SH BUGS
+For issues and feature requests please use:
+.LP
+.RS
+\fI@PACKAGE_URL@\fP
+.RE
+.LP
+For question and help please use:
+.LP
+.RS
+\fI@PACKAGE_BUGREPORT@\fP
+.RE
+.LP
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/***
+ *** DNS Resolution Performance Testing Tool
+ ***/
+
+#include "config.h"
+
+#include "datafile.h"
+#include "dns.h"
+#include "log.h"
+#include "net.h"
+#include "opt.h"
+#include "util.h"
+#include "os.h"
+#include "list.h"
+#include "result.h"
+#include "buffer.h"
+
+#include <errno.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <openssl/ssl.h>
+#include <openssl/conf.h>
+#include <openssl/err.h>
+#include <signal.h>
+
+/*
+ * Global stuff
+ */
+
+#define DEFAULT_SERVER_NAME "127.0.0.1"
+#define DEFAULT_SERVER_PORT 53
+#define DEFAULT_SERVER_DOT_PORT 853
+#define DEFAULT_SERVER_DOH_PORT 443
+#define DEFAULT_SERVER_PORTS "udp/tcp 53, DoT 853 or DoH 443"
+#define DEFAULT_LOCAL_PORT 0
+#define DEFAULT_SOCKET_BUFFER 32
+#define DEFAULT_TIMEOUT 45
+#define DEFAULT_MAX_OUTSTANDING (64 * 1024)
+#define DEFAULT_MAX_FALL_BEHIND 1000
+
+#define MAX_INPUT_DATA (64 * 1024) + 2
+
+#define TIMEOUT_CHECK_TIME 5000000
+
+#define DNS_RCODE_NOERROR 0
+#define DNS_RCODE_NXDOMAIN 3
+
+struct query_info;
+
+typedef perf_list(struct query_info) query_list;
+
+typedef struct query_info {
+ uint64_t sent_timestamp;
+ bool is_inprogress;
+
+ /*
+ * This link links the query into the list of outstanding
+ * queries or the list of available query IDs.
+ */
+ perf_link(struct query_info);
+ /*
+ * The list this query is on.
+ */
+ query_list* list;
+} query_info;
+
+static query_list outstanding_list;
+static query_list instanding_list;
+
+static query_info* queries;
+
+static perf_sockaddr_t server_addr;
+static perf_sockaddr_t local_addr;
+static unsigned int nsocks;
+static struct perf_net_socket** socks;
+static enum perf_net_mode mode;
+
+static int dummypipe[2];
+
+static uint64_t query_timeout;
+static bool edns;
+static bool dnssec;
+
+static perf_ednsoption_t* edns_option = 0;
+
+static perf_datafile_t* input;
+
+/* The target traffic level at the end of the ramp-up */
+double max_qps = 100000.0;
+
+/* The time period over which we ramp up traffic */
+#define DEFAULT_RAMP_TIME 60
+static uint64_t ramp_time;
+
+/* How long to send constant traffic after the initial ramp-up */
+#define DEFAULT_SUSTAIN_TIME 0
+static uint64_t sustain_time;
+
+/* How long to wait for responses after sending traffic */
+static uint64_t wait_time = 40 * MILLION;
+
+/* Total duration of the traffic-sending part of the test */
+static uint64_t traffic_time;
+
+/* Total duration of the test */
+static uint64_t end_time;
+
+/* Interval between plot data points, in microseconds */
+#define DEFAULT_BUCKET_INTERVAL 0.5
+static uint64_t bucket_interval;
+
+/* The number of plot data points */
+static int n_buckets;
+
+/* The plot data file */
+static const char* plotfile = "resperf.gnuplot";
+
+/* The largest acceptable query loss when reporting max throughput */
+static double max_loss_percent = 100.0;
+
+/* The maximum number of outstanding queries */
+static unsigned int max_outstanding;
+
+static uint64_t num_queries_sent;
+static uint64_t num_queries_outstanding;
+static uint64_t num_responses_received;
+static uint64_t num_queries_timed_out;
+static uint64_t rcodecounts[16];
+static uint64_t num_conn_completed;
+static uint64_t num_conn_attempts;
+static uint64_t num_response_unexpected;
+
+static uint64_t time_now;
+static uint64_t time_of_program_start;
+static uint64_t time_of_end_of_run;
+
+/*
+ * The last plot data point containing actual data; this can
+ * be less than than (n_buckets - 1) if the traffic sending
+ * phase is cut short
+ */
+static int last_bucket_used;
+
+/*
+ * The statistics for queries sent during one bucket_interval
+ * of the traffic sending phase.
+ */
+typedef struct {
+ int queries;
+ int responses;
+ int failures;
+ double latency_sum;
+
+ int connections;
+ double conn_latency_sum;
+} ramp_bucket;
+
+/* Pointer to array of n_buckets ramp_bucket structures */
+static ramp_bucket* buckets;
+
+enum phase {
+ /*
+ * The ramp-up phase: we are steadily increasing traffic.
+ */
+ PHASE_RAMP,
+ /*
+ * The sustain phase: we are sending traffic at a constant
+ * rate.
+ */
+ PHASE_SUSTAIN,
+ /*
+ * The wait phase: we have stopped sending queries and are
+ * just waiting for any remaining responses.
+ */
+ PHASE_WAIT
+};
+static enum phase phase = PHASE_RAMP;
+
+/* The time when the sustain/wait phase began */
+static uint64_t sustain_phase_began, wait_phase_began;
+
+static perf_tsigkey_t* tsigkey;
+
+static bool verbose;
+static unsigned int max_fall_behind;
+
+static perf_suppress_t suppress;
+
+const char* progname = "resperf";
+
+static char*
+stringify(double value, int precision)
+{
+ static char buf[20];
+
+ snprintf(buf, sizeof(buf), "%.*f", precision, value);
+ return buf;
+}
+
+static void perf__net_event(struct perf_net_socket* sock, perf_socket_event_t event, uint64_t elapsed_time);
+static void perf__net_sent(struct perf_net_socket* sock, uint16_t qid);
+
+static ramp_bucket* init_buckets(int n)
+{
+ ramp_bucket* p;
+ int i;
+
+ if (!(p = calloc(n, sizeof(*p)))) {
+ perf_log_fatal("out of memory");
+ return 0; // fix clang scan-build
+ }
+ for (i = 0; i < n; i++) {
+ p[i].queries = p[i].responses = p[i].failures = 0;
+ p[i].latency_sum = 0.0;
+ }
+ return p;
+}
+
+static void setup(int argc, char** argv)
+{
+ const char* family = NULL;
+ const char* server_name = DEFAULT_SERVER_NAME;
+ in_port_t server_port = 0;
+ const char* local_name = NULL;
+ in_port_t local_port = DEFAULT_LOCAL_PORT;
+ const char* filename = NULL;
+ const char* tsigkey_str = NULL;
+ int sock_family;
+ unsigned int bufsize;
+ unsigned int i;
+ const char* _mode = 0;
+ const char* edns_option_str = NULL;
+ const char* doh_uri = DEFAULT_DOH_URI;
+ const char* doh_method = DEFAULT_DOH_METHOD;
+ const char* tls_sni = 0;
+ const char* local_suppress = 0;
+
+ size_t num_queries_per_conn = 0;
+
+ sock_family = AF_UNSPEC;
+ server_port = 0;
+ local_port = DEFAULT_LOCAL_PORT;
+ bufsize = DEFAULT_SOCKET_BUFFER;
+ query_timeout = DEFAULT_TIMEOUT * MILLION;
+ ramp_time = DEFAULT_RAMP_TIME * MILLION;
+ sustain_time = DEFAULT_SUSTAIN_TIME * MILLION;
+ bucket_interval = DEFAULT_BUCKET_INTERVAL * MILLION;
+ max_outstanding = DEFAULT_MAX_OUTSTANDING;
+ nsocks = 1;
+ mode = sock_udp;
+ verbose = false;
+ max_fall_behind = DEFAULT_MAX_FALL_BEHIND;
+
+ perf_opt_add('f', perf_opt_string, "family",
+ "address family of DNS transport, inet or inet6", "any",
+ &family);
+ perf_opt_add('M', perf_opt_string, "mode", "set transport mode: udp, tcp, dot or doh", "udp", &_mode);
+ perf_opt_add('s', perf_opt_string, "server_addr",
+ "the server to query", DEFAULT_SERVER_NAME, &server_name);
+ perf_opt_add('p', perf_opt_port, "port",
+ "the port on which to query the server",
+ DEFAULT_SERVER_PORTS, &server_port);
+ perf_opt_add('a', perf_opt_string, "local_addr",
+ "the local address from which to send queries", NULL,
+ &local_name);
+ perf_opt_add('x', perf_opt_port, "local_port",
+ "the local port from which to send queries",
+ stringify(DEFAULT_LOCAL_PORT, 0), &local_port);
+ perf_opt_add('d', perf_opt_string, "datafile",
+ "the input data file", "stdin", &filename);
+ perf_opt_add('t', perf_opt_timeval, "timeout",
+ "the timeout for query completion in seconds",
+ stringify(DEFAULT_TIMEOUT, 0), &query_timeout);
+ perf_opt_add('b', perf_opt_uint, "buffer_size",
+ "socket send/receive buffer size in kilobytes", NULL,
+ &bufsize);
+ perf_opt_add('e', perf_opt_boolean, NULL,
+ "enable EDNS 0", NULL, &edns);
+ perf_opt_add('E', perf_opt_string, "code:value",
+ "send EDNS option", NULL, &edns_option_str);
+ perf_opt_add('D', perf_opt_boolean, NULL,
+ "set the DNSSEC OK bit (implies EDNS)", NULL, &dnssec);
+ perf_opt_add('y', perf_opt_string, "[alg:]name:secret",
+ "the TSIG algorithm, name and secret", NULL, &tsigkey_str);
+ perf_opt_add('i', perf_opt_timeval, "plot_interval",
+ "the time interval between plot data points, in seconds",
+ stringify(DEFAULT_BUCKET_INTERVAL, 1), &bucket_interval);
+ perf_opt_add('m', perf_opt_double, "max_qps",
+ "the maximum number of queries per second",
+ stringify(max_qps, 0), &max_qps);
+ perf_opt_add('P', perf_opt_string, "plotfile",
+ "the name of the plot data file", plotfile, &plotfile);
+ perf_opt_add('r', perf_opt_timeval, "ramp_time",
+ "the ramp-up time in seconds",
+ stringify(DEFAULT_RAMP_TIME, 0), &ramp_time);
+ perf_opt_add('c', perf_opt_timeval, "constant_traffic_time",
+ "how long to send constant traffic, in seconds",
+ stringify(DEFAULT_SUSTAIN_TIME, 0), &sustain_time);
+ perf_opt_add('L', perf_opt_double, "max_query_loss",
+ "the maximum acceptable query loss, in percent",
+ stringify(max_loss_percent, 0), &max_loss_percent);
+ perf_opt_add('C', perf_opt_uint, "clients",
+ "the number of clients to act as", stringify(1, 0), &nsocks);
+ perf_opt_add('q', perf_opt_uint, "num_outstanding",
+ "the maximum number of queries outstanding",
+ stringify(DEFAULT_MAX_OUTSTANDING, 0), &max_outstanding);
+ perf_opt_add('v', perf_opt_boolean, NULL,
+ "verbose: report additional information to stdout",
+ NULL, &verbose);
+ bool log_stdout = false;
+ perf_opt_add('W', perf_opt_boolean, NULL, "log warnings and errors to stdout instead of stderr", NULL, &log_stdout);
+ bool reopen_datafile = false;
+ perf_opt_add('R', perf_opt_boolean, NULL, "reopen datafile on end, allow for infinit use of it", NULL, &reopen_datafile);
+ perf_opt_add('F', perf_opt_zpint, "fall_behind", "the maximum number of queries that is allowed to fall behind, zero to disable",
+ stringify(DEFAULT_MAX_FALL_BEHIND, 0), &max_fall_behind);
+ perf_long_opt_add("doh-uri", perf_opt_string, "doh_uri",
+ "the URI to use for DNS-over-HTTPS", DEFAULT_DOH_URI, &doh_uri);
+ perf_long_opt_add("doh-method", perf_opt_string, "doh_method",
+ "the HTTP method to use for DNS-over-HTTPS: GET or POST", DEFAULT_DOH_METHOD, &doh_method);
+ perf_long_opt_add("tls-sni", perf_opt_string, "tls_sni",
+ "the TLS SNI to use for TLS connections", NULL, &tls_sni);
+ perf_long_opt_add("suppress", perf_opt_string, "message[,message,...]",
+ "suppress messages/warnings, see dnsperf(1) man-page for list of message types", NULL, &local_suppress);
+ perf_long_opt_add("num-queries-per-conn", perf_opt_uint, "queries",
+ "Number of queries to send per connection", NULL, &num_queries_per_conn);
+
+ perf_opt_parse(argc, argv);
+
+ if (log_stdout) {
+ perf_log_tostdout();
+ }
+
+ suppress = perf_opt_parse_suppress(local_suppress);
+
+ if (_mode != 0)
+ mode = perf_net_parsemode(_mode);
+
+ if (!server_port) {
+ switch (mode) {
+ case sock_doh:
+ server_port = DEFAULT_SERVER_DOH_PORT;
+ break;
+ case sock_dot:
+ server_port = DEFAULT_SERVER_DOT_PORT;
+ break;
+ default:
+ server_port = DEFAULT_SERVER_PORT;
+ break;
+ }
+ }
+
+ if (tls_sni) {
+ perf_net_tls_sni = tls_sni;
+ }
+
+ if (doh_uri) {
+ perf_net_doh_parse_uri(doh_uri);
+ }
+ if (doh_method) {
+ perf_net_doh_parse_method(doh_method);
+ }
+ perf_net_doh_set_max_concurrent_streams(max_outstanding);
+
+ if (max_outstanding > nsocks * DEFAULT_MAX_OUTSTANDING)
+ perf_log_fatal("number of outstanding packets (%u) must not "
+ "be more than 64K per client",
+ max_outstanding);
+
+ if (ramp_time + sustain_time == 0)
+ perf_log_fatal("rampup_time and constant_traffic_time must not "
+ "both be 0");
+
+ perf_list_init(outstanding_list);
+ perf_list_init(instanding_list);
+ if (!(queries = calloc(max_outstanding, sizeof(query_info)))) {
+ perf_log_fatal("out of memory");
+ }
+ for (i = 0; i < max_outstanding; i++) {
+ perf_link_init(&queries[i]);
+ perf_list_append(instanding_list, &queries[i]);
+ queries[i].list = &instanding_list;
+ }
+
+ if (family != NULL)
+ sock_family = perf_net_parsefamily(family);
+ perf_net_parseserver(sock_family, server_name, server_port, &server_addr);
+ perf_net_parselocal(server_addr.sa.sa.sa_family, local_name,
+ local_port, &local_addr);
+
+ input = perf_datafile_open(filename, input_format_text_query);
+ if (reopen_datafile) {
+ perf_datafile_setmaxruns(input, -1);
+ }
+
+ if (dnssec || edns_option_str)
+ edns = true;
+
+ if (tsigkey_str != NULL)
+ tsigkey = perf_tsig_parsekey(tsigkey_str);
+
+ if (edns_option_str != NULL)
+ edns_option = perf_edns_parseoption(edns_option_str);
+
+ traffic_time = ramp_time + sustain_time;
+ end_time = traffic_time + wait_time;
+
+ n_buckets = (traffic_time + bucket_interval - 1) / bucket_interval;
+ buckets = init_buckets(n_buckets);
+
+ time_now = perf_get_time();
+ time_of_program_start = time_now;
+
+ if (!(socks = calloc(nsocks, sizeof(*socks)))) {
+ perf_log_fatal("out of memory");
+ }
+ for (i = 0; i < nsocks; i++) {
+ socks[i] = perf_net_opensocket(mode, &server_addr, &local_addr, i, bufsize, (void*)(intptr_t)i, perf__net_sent, perf__net_event);
+ if (!socks[i]) {
+ perf_log_fatal("perf_net_opensocket(): no socket returned, out of memory?");
+ }
+ if (num_queries_per_conn && socks[i]->num_queries_per_conn) {
+ socks[i]->num_queries_per_conn(socks[i], num_queries_per_conn, query_timeout);
+ }
+ }
+}
+
+static void
+cleanup(void)
+{
+ unsigned int i;
+
+ perf_datafile_close(&input);
+ for (i = 0; i < nsocks; i++) {
+ perf_net_stats_compile(mode, socks[i]);
+ (void)perf_net_close(socks[i]);
+ }
+ close(dummypipe[0]);
+ close(dummypipe[1]);
+
+ if (edns_option)
+ perf_edns_destroyoption(&edns_option);
+}
+
+/* Find the ramp_bucket for queries sent at time "when" */
+
+static ramp_bucket*
+find_bucket(uint64_t when)
+{
+ uint64_t sent_at = when - time_of_program_start;
+ int i = (int)((n_buckets * sent_at) / traffic_time);
+ /*
+ * Guard against array bounds violations due to roundoff
+ * errors or scheduling jitter
+ */
+ if (i < 0)
+ i = 0;
+ if (i > n_buckets - 1)
+ i = n_buckets - 1;
+ return &buckets[i];
+}
+
+static void perf__net_event(struct perf_net_socket* sock, perf_socket_event_t event, uint64_t elapsed_time)
+{
+ ramp_bucket* b = find_bucket(time_now);
+
+ switch (event) {
+ case perf_socket_event_reconnected:
+ case perf_socket_event_connected:
+ b->connections++;
+ b->conn_latency_sum += elapsed_time / (double)MILLION;
+ num_conn_completed++;
+ break;
+
+ case perf_socket_event_reconnecting:
+ case perf_socket_event_connecting:
+ num_conn_attempts++;
+ break;
+
+ default:
+ break;
+ }
+}
+
+static void perf__net_sent(struct perf_net_socket* sock, uint16_t qid)
+{
+ ramp_bucket* b = find_bucket(time_now);
+
+ b->queries++;
+
+ size_t idx = (size_t)qid * nsocks + (intptr_t)sock->data;
+ assert(idx < max_outstanding);
+ queries[idx].sent_timestamp = time_now;
+}
+
+/*
+ * print_statistics:
+ * Print out statistics based on the results of the test
+ */
+static void
+print_statistics(void)
+{
+ int i;
+ double max_throughput;
+ double loss_at_max_throughput;
+ bool first_rcode;
+ uint64_t run_time = time_of_end_of_run - time_of_program_start;
+
+ printf("\nStatistics:\n\n");
+
+ printf(" Queries sent: %" PRIu64 "\n",
+ num_queries_sent);
+ printf(" Queries completed: %" PRIu64 "\n",
+ num_responses_received);
+ printf(" Queries lost: %" PRIu64 "\n",
+ num_queries_sent - num_responses_received);
+ if (num_response_unexpected > 0)
+ printf(" Unexpected IDs: %" PRIu64 "\n",
+ num_response_unexpected);
+ printf(" Response codes: ");
+ first_rcode = true;
+ for (i = 0; i < 16; i++) {
+ if (rcodecounts[i] == 0)
+ continue;
+ if (first_rcode)
+ first_rcode = false;
+ else
+ printf(", ");
+ printf("%s %" PRIu64 " (%.2lf%%)",
+ perf_dns_rcode_strings[i], rcodecounts[i],
+ (rcodecounts[i] * 100.0) / num_responses_received);
+ }
+ printf("\n\n");
+ printf(" Run time (s): %u.%06u\n",
+ (unsigned int)(run_time / MILLION),
+ (unsigned int)(run_time % MILLION));
+
+ /* Find the maximum throughput, subject to the -L option */
+ max_throughput = 0.0;
+ loss_at_max_throughput = 0.0;
+ for (i = 0; i <= last_bucket_used; i++) {
+ ramp_bucket* b = &buckets[i];
+ double responses_per_sec = b->responses / (bucket_interval / (double)MILLION);
+ double loss = b->queries ? (b->queries - b->responses) / (double)b->queries : 0.0;
+ double loss_percent = loss * 100.0;
+ if (loss_percent > max_loss_percent)
+ break;
+ if (responses_per_sec > max_throughput) {
+ max_throughput = responses_per_sec;
+ loss_at_max_throughput = loss_percent;
+ }
+ }
+ printf(" Maximum throughput: %.6lf qps\n", max_throughput);
+ printf(" Lost at that point: %.2f%%\n", loss_at_max_throughput);
+ printf("\n");
+ printf(" Connection attempts: %" PRIu64 " (%" PRIu64 " successful, %.2lf%%)\n\n",
+ num_conn_attempts,
+ num_conn_completed,
+ PERF_SAFE_DIV(100.0 * num_conn_completed, num_conn_attempts));
+}
+
+/*
+ * Send a query based on a line of input.
+ * Return PERF_R_NOMORE if we ran out of query IDs.
+ */
+static perf_result_t
+do_one_line(perf_buffer_t* lines, perf_buffer_t* msg)
+{
+ query_info* q;
+ unsigned int qid;
+ unsigned int sock;
+ perf_region_t used;
+ unsigned char* base;
+ unsigned int length;
+ perf_result_t result;
+
+ q = perf_list_head(instanding_list);
+ if (!q)
+ return (PERF_R_NOMORE);
+ qid = (q - queries) / nsocks;
+ sock = (q - queries) % nsocks;
+
+ while (q->is_inprogress) {
+ if (perf_net_sockready(socks[sock], dummypipe[0], TIMEOUT_CHECK_TIME) == -1) {
+ if (errno == EINPROGRESS) {
+ if (verbose && !suppress.congestion) {
+ perf_log_warning("network congested, packet sending in progress");
+ }
+ } else {
+ if (verbose && !suppress.sockready) {
+ char __s[256];
+ perf_log_warning("failed to check socket readiness: %s", perf_strerror_r(errno, __s, sizeof(__s)));
+ }
+ }
+ return (PERF_R_FAILURE);
+ }
+
+ q->is_inprogress = false;
+ perf_list_unlink(instanding_list, q);
+ perf_list_prepend(outstanding_list, q);
+ q->list = &outstanding_list;
+
+ num_queries_sent++;
+ num_queries_outstanding++;
+
+ q = perf_list_head(instanding_list);
+ if (!q)
+ return (PERF_R_NOMORE);
+ qid = (q - queries) / nsocks;
+ sock = (q - queries) % nsocks;
+ }
+
+ switch (perf_net_sockready(socks[sock], dummypipe[0], TIMEOUT_CHECK_TIME)) {
+ case 0:
+ if (verbose && !suppress.sockready) {
+ perf_log_warning("failed to send packet: socket %d not ready", sock);
+ }
+ return (PERF_R_FAILURE);
+ case -1:
+ if (errno == EINPROGRESS) {
+ if (verbose && !suppress.congestion) {
+ perf_log_warning("network congested, packet sending in progress");
+ }
+ } else if (!suppress.sockready) {
+ perf_log_warning("failed to send packet: socket %d not ready", sock);
+ }
+ return (PERF_R_FAILURE);
+ default:
+ break;
+ }
+
+ perf_buffer_clear(lines);
+ result = perf_datafile_next(input, lines);
+ if (result != PERF_R_SUCCESS)
+ perf_log_fatal("ran out of query data");
+ perf_buffer_usedregion(lines, &used);
+
+ perf_buffer_clear(msg);
+ result = perf_dns_buildrequest(&used, qid,
+ edns, dnssec, false,
+ tsigkey, edns_option,
+ msg);
+ if (result != PERF_R_SUCCESS)
+ return (result);
+
+ q->sent_timestamp = time_now;
+
+ base = perf_buffer_base(msg);
+ length = perf_buffer_usedlength(msg);
+ if (perf_net_sendto(socks[sock], qid, base, length, 0,
+ &server_addr.sa.sa, server_addr.length)
+ < 1) {
+ if (errno == EINPROGRESS) {
+ if (verbose && !suppress.congestion) {
+ perf_log_warning("network congested, packet sending in progress");
+ }
+ q->is_inprogress = true;
+ } else {
+ if (verbose && !suppress.sendfailed) {
+ char __s[256];
+ perf_log_warning("failed to send packet: %s", perf_strerror_r(errno, __s, sizeof(__s)));
+ }
+ }
+ return (PERF_R_FAILURE);
+ }
+
+ perf_list_unlink(instanding_list, q);
+ perf_list_prepend(outstanding_list, q);
+ q->list = &outstanding_list;
+
+ num_queries_sent++;
+ num_queries_outstanding++;
+
+ return PERF_R_SUCCESS;
+}
+
+static void
+enter_sustain_phase(void)
+{
+ phase = PHASE_SUSTAIN;
+ if (sustain_time != 0.0)
+ printf("[Status] Ramp-up done, sending constant traffic\n");
+ sustain_phase_began = time_now;
+}
+
+static void
+enter_wait_phase(void)
+{
+ phase = PHASE_WAIT;
+ printf("[Status] Waiting for more responses\n");
+ wait_phase_began = time_now;
+}
+
+/*
+ * try_process_response:
+ *
+ * Receive from the given socket & process an individual response packet.
+ * Remove it from the list of open queries (status[]) and decrement the
+ * number of outstanding queries if it matches an open query.
+ */
+static void
+try_process_response(unsigned int sockindex)
+{
+ unsigned char packet_buffer[MAX_EDNS_PACKET];
+ uint16_t* packet_header;
+ uint16_t qid, rcode;
+ query_info* q;
+ double latency;
+ ramp_bucket* b;
+ int n;
+
+ if (perf_net_sockready(socks[sockindex], dummypipe[0], TIMEOUT_CHECK_TIME) == -1) {
+ if (errno != EINPROGRESS) {
+ if (verbose && !suppress.sockready) {
+ char __s[256];
+ perf_log_warning("failed to check socket readiness: %s", perf_strerror_r(errno, __s, sizeof(__s)));
+ }
+ }
+ }
+
+ packet_header = (uint16_t*)packet_buffer;
+ n = perf_net_recv(socks[sockindex], packet_buffer, sizeof(packet_buffer), 0);
+ if (n < 0) {
+ if (errno == EAGAIN || errno == EINTR) {
+ return;
+ } else {
+ char __s[256];
+ perf_log_fatal("failed to receive packet: %s", perf_strerror_r(errno, __s, sizeof(__s)));
+ }
+ } else if (!n) {
+ // Treat connection closed like try again until reconnection features are in
+ return;
+ } else if (n < 4) {
+ perf_log_warning("received short response");
+ return;
+ }
+
+ qid = ntohs(packet_header[0]);
+ rcode = ntohs(packet_header[1]) & 0xF;
+
+ size_t idx = qid * nsocks + sockindex;
+ if (idx >= max_outstanding || queries[idx].list != &outstanding_list) {
+ if (!suppress.unexpected) {
+ perf_log_warning("received a response with an unexpected id: %u", qid);
+ }
+ num_response_unexpected++;
+ return;
+ }
+ q = &queries[idx];
+
+ perf_list_unlink(outstanding_list, q);
+ perf_list_append(instanding_list, q);
+ q->list = &instanding_list;
+
+ num_queries_outstanding--;
+
+ latency = (time_now - q->sent_timestamp) / (double)MILLION;
+ b = find_bucket(q->sent_timestamp);
+ b->responses++;
+ if (!(rcode == DNS_RCODE_NOERROR || rcode == DNS_RCODE_NXDOMAIN))
+ b->failures++;
+ b->latency_sum += latency;
+ num_responses_received++;
+ rcodecounts[rcode]++;
+}
+
+static void
+retire_old_queries(void)
+{
+ query_info* q;
+
+ while (true) {
+ q = perf_list_tail(outstanding_list);
+ if (q == NULL || (time_now - q->sent_timestamp) < query_timeout)
+ break;
+ perf_list_unlink(outstanding_list, q);
+ perf_list_append(instanding_list, q);
+ q->list = &instanding_list;
+
+ num_queries_outstanding--;
+ num_queries_timed_out++;
+ }
+}
+
+static inline int
+num_scheduled(uint64_t time_since_start)
+{
+ if (phase == PHASE_RAMP) {
+ return 0.5 * max_qps * (double)time_since_start * time_since_start / (ramp_time * MILLION);
+ } else { /* PHASE_SUSTAIN */
+ return 0.5 * max_qps * (ramp_time / (double)MILLION) + max_qps * (time_since_start - ramp_time) / (double)MILLION;
+ }
+}
+
+int main(int argc, char** argv)
+{
+ int i;
+ FILE* plotf;
+ perf_buffer_t lines, msg;
+ char input_data[MAX_INPUT_DATA];
+ unsigned char outpacket_buffer[MAX_EDNS_PACKET];
+ unsigned int max_packet_size;
+ unsigned int current_sock;
+ perf_result_t result;
+
+ printf("DNS Resolution Performance Testing Tool\n"
+ "Version " PACKAGE_VERSION "\n\n");
+
+ (void)SSL_library_init();
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
+ SSL_load_error_strings();
+ OPENSSL_config(0);
+#endif
+
+ setup(argc, argv);
+
+ if (pipe(dummypipe) < 0)
+ perf_log_fatal("creating pipe");
+
+ switch (mode) {
+ case sock_tcp:
+ case sock_dot:
+ case sock_doh:
+ // block SIGPIPE for TCP/DOT mode, if connection is closed it will generate a signal
+ perf_os_blocksignal(SIGPIPE, true);
+ break;
+ default:
+ break;
+ }
+
+ perf_buffer_init(&lines, input_data, sizeof(input_data));
+
+ max_packet_size = edns ? MAX_EDNS_PACKET : MAX_UDP_PACKET;
+ perf_buffer_init(&msg, outpacket_buffer, max_packet_size);
+
+ printf("[Status] Command line: %s", progname);
+ for (i = 1; i < argc; i++) {
+ printf(" %s", argv[i]);
+ }
+ printf("\n");
+
+ printf("[Status] Sending\n");
+
+ int try_responses = (max_qps / max_outstanding) + 1;
+ current_sock = 0;
+ for (;;) {
+ int should_send;
+ uint64_t time_since_start = time_now - time_of_program_start;
+ switch (phase) {
+ case PHASE_RAMP:
+ if (time_since_start >= ramp_time)
+ enter_sustain_phase();
+ break;
+ case PHASE_SUSTAIN:
+ if (time_since_start >= traffic_time)
+ enter_wait_phase();
+ break;
+ case PHASE_WAIT:
+ if (time_since_start >= end_time || perf_list_empty(outstanding_list))
+ goto end_loop;
+ break;
+ }
+ if (phase != PHASE_WAIT) {
+ should_send = num_scheduled(time_since_start) - num_queries_sent;
+ if (max_fall_behind && should_send >= max_fall_behind) {
+ printf("[Status] Fell behind by %d queries, "
+ "ending test at %.0f qps\n",
+ should_send, (max_qps * time_since_start) / ramp_time);
+ enter_wait_phase();
+ }
+ if (should_send > 0) {
+ result = do_one_line(&lines, &msg);
+ if (result == PERF_R_SUCCESS)
+ find_bucket(time_now)->queries++;
+ if (result == PERF_R_NOMORE) {
+ printf("[Status] Reached %u outstanding queries\n",
+ max_outstanding);
+ enter_wait_phase();
+ }
+ }
+ }
+ for (i = try_responses; i--;) {
+ try_process_response(current_sock++);
+ if (current_sock >= nsocks)
+ current_sock = 0;
+ }
+ retire_old_queries();
+ time_now = perf_get_time();
+ }
+end_loop:
+ time_now = perf_get_time();
+ time_of_end_of_run = time_now;
+
+ printf("[Status] Testing complete\n");
+
+ plotf = fopen(plotfile, "w");
+ if (!plotf) {
+ char __s[256];
+ perf_log_fatal("could not open %s: %s", plotfile, perf_strerror_r(errno, __s, sizeof(__s)));
+ return 0; // fix clang scan-build
+ }
+
+ /* Print column headers */
+ fprintf(plotf, "# time target_qps actual_qps responses_per_sec failures_per_sec avg_latency"
+ " connections conn_avg_latency\n");
+
+ /* Don't print unused buckets */
+ last_bucket_used = find_bucket(wait_phase_began) - buckets;
+
+ /* Don't print a partial bucket at the end */
+ if (last_bucket_used > 0)
+ --last_bucket_used;
+
+ for (i = 0; i <= last_bucket_used; i++) {
+ double t = (i + 0.5) * traffic_time / (n_buckets * (double)MILLION);
+ double ramp_dtime = ramp_time / (double)MILLION;
+ double target_qps = t <= ramp_dtime ? (t / ramp_dtime) * max_qps : max_qps;
+ double latency = buckets[i].responses ? buckets[i].latency_sum / buckets[i].responses : 0;
+ double interval = bucket_interval / (double)MILLION;
+
+ double conn_latency = buckets[i].connections ? buckets[i].conn_latency_sum / buckets[i].connections : 0;
+
+ fprintf(plotf, "%7.3f %8.2f %8.2f %8.2f %8.2f %8.6f %8.2f %8.6f\n",
+ t,
+ target_qps,
+ (double)buckets[i].queries / interval,
+ (double)buckets[i].responses / interval,
+ (double)buckets[i].failures / interval,
+ latency,
+ (double)buckets[i].connections / interval,
+ conn_latency);
+ }
+
+ fclose(plotf);
+ print_statistics();
+ perf_net_stats_init(mode);
+ cleanup();
+ perf_net_stats_print(mode, false);
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
+ ERR_free_strings();
+#endif
+
+ return 0;
+}
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef PERF_RESULT_H
+#define PERF_RESULT_H 1
+
+#include <assert.h>
+
+typedef unsigned int perf_result_t;
+
+#define PERF_R_SUCCESS 0
+#define PERF_R_FAILURE 1
+#define PERF_R_CANCELED 2
+#define PERF_R_EOF 3
+#define PERF_R_INVALIDFILE 4
+#define PERF_R_NOMORE 5
+#define PERF_R_NOSPACE 6
+#define PERF_R_TIMEDOUT 7
+
+#define PERF_R_INVALIDUPDATE 100
+
+#endif
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "config.h"
+
+#include "strerror.h"
+
+#include <string.h>
+#include <stdio.h>
+
+const char* perf_strerror_r(int errnum, char* str, size_t len)
+{
+#if ((_POSIX_C_SOURCE >= 200112L) && !_GNU_SOURCE) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
+ if (strerror_r(errnum, str, len)) {
+ (void)snprintf(str, len, "Error %d", errnum);
+ }
+ return str;
+#else
+ return strerror_r(errnum, str, len);
+#endif
+}
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef PERF_STRERROR_H
+#define PERF_STRERROR_H 1
+
+#include <stddef.h>
+
+const char* perf_strerror_r(int errnum, char* str, size_t len);
+
+#endif
--- /dev/null
+*.blob -diff
--- /dev/null
+MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
+
+CLEANFILES = test*.log test*.trs \
+ test2.out test4.out test4err.out key.pem cert.pem test6.out \
+ 10queries.tmp.blob 10queries.tmp.out empty.out emptypayload.out \
+ largesttcp.out largestudp.out missingpayload.out querywithcookie.out \
+ shortpayload.out tooshortlength.out twoquerieswithnsid.out
+
+TESTS = test1.sh test2.sh test3.sh test4.sh test5.sh test6.sh test7.sh
+
+EXTRA_DIST = $(TESTS) \
+ datafile datafile2 updatefile datafile3 datafile4 datafile5 datafile6 \
+ empty.blob emptypayload.blob largesttcp.blob largestudp.blob \
+ missingpayload.blob querywithcookie.blob shortpayload.blob \
+ tooshortlength.blob twoquerieswithnsid.blob
--- /dev/null
+google.com A
+google.com AAAA
--- /dev/null
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
+google.com A
+google.com AAAA
--- /dev/null
+. A
+google.com. A
--- /dev/null
+api-read.facebook.com.\002\004\003\002\002\002\002\005\004\004\003\004\006\005\006\006\006\005\006\006\006\007\009\008\006\007\009\007\006\006\008\011\008\009\010\010\010\010\010\006\008\011\012\011\010\012\009\010\010\010\255\219. A
+valid\.quote.com A
+invalid\0quote.com A
+tab\009.quote.com A
+space\032.quote.com A
\ No newline at end of file
--- /dev/null
+google.com TYPE0
+google.com TYPE1
+google.com TYPE28
+google.com TYPE99999999
+google.com TYPE99999999 ignored
+google.com ANY
--- /dev/null
+\" A
+\001 A
+\". A
+a\". A
+\"a. A
+a\"a. A
+a\"a\". A
+\!\"a\". A
+\001. A
+a\001. A
+\001a. A
+a\001a. A
+a\001\001\001a. A
+a\001a\001a\001. A
+\".\". A
+a\".a\". A
+\"a.\"a. A
+a\"a.a\"a. A
+a\"a\".a\"a\". A
+\!\"a\".\!\"a\". A
+\001.\001. A
+a\001.a\001. A
+\001a.\001a. A
+a\001a.a\001a. A
+a\001\001\001a.a\001\001\001a. A
+a\001a\001a\001.a\001a\001a\001. A
--- /dev/null
+#!/bin/sh
+
+gen-largest-blob.py 20065 65503 largesttcp.blob
+gen-largest-blob.py 20065 65475 largestudp.blob
--- /dev/null
+#!/usr/bin/env python3
+
+import sys
+import dns.message
+import struct
+
+m = dns.message.make_query('.', 'TYPE666')
+m.id = int(sys.argv[1])
+m.use_edns(0, options=[dns.edns.GenericOption(dns.edns.OptionType.PADDING, '\x00' * int(sys.argv[2]))])
+binary = m.to_wire(max_size=65535)
+binary = struct.pack('>H', len(binary)) + binary
+open(sys.argv[3], 'wb').write(binary)
--- /dev/null
+#!/bin/sh -xe
+
+../dnsperf -h
+../resperf -h
+
+! ../dnsperf -O suppress
+! ../dnsperf -O suppress=
+! ../resperf -O suppress
+! ../resperf -O suppress=
+
+# test for broken long opt in v2.11.0
+../dnsperf -O suppress=test 2>&1 |grep -q "unknown message type to suppress: test"
+# ...and in v2.11.1, issue #234
+../dnsperf -O doh-uri=https://blahblah.com/dns-query -O suppress=timeouts -h
\ No newline at end of file
--- /dev/null
+#!/bin/sh -xe
+
+test "$TEST_DNSPERF_WITH_NETWORK" = "1" || exit 0
+
+for ip in 1.1.1.1 2606:4700:4700::1111; do
+
+echo "google.com A" | ../dnsperf -vvv -s $ip -m udp >test2.out
+cat test2.out
+grep -q "Queries sent: *1" test2.out
+echo "google.com A" | ../dnsperf -vvv -s $ip -e -E 12345:0a0a0a0a -m udp >test2.out
+cat test2.out
+grep -q "Queries sent: *1" test2.out
+../dnsperf -vvv -s $ip -d "$srcdir/datafile" -n 2 -m udp >test2.out
+cat test2.out
+grep -q "Queries sent: *4" test2.out
+../dnsperf -s $ip -d "$srcdir/datafile" -n 1 -m tcp >test2.out
+cat test2.out
+grep -q "Queries sent: *2" test2.out
+../dnsperf -s $ip -d "$srcdir/datafile" -n 1 -m dot >test2.out
+cat test2.out
+grep -q "Queries sent: *2" test2.out
+../dnsperf -s $ip -d "$srcdir/datafile" -n 1 -m dot >test2.out
+cat test2.out
+grep -q "Queries sent: *2" test2.out
+
+../dnsperf -s $ip -d "$srcdir/datafile3" -n 1 -m dot >test2.out
+cat test2.out
+grep -q "Queries sent: *2" test2.out
+
+../dnsperf -s $ip -d "$srcdir/datafile" -n 1 -e >test2.out
+cat test2.out
+grep -q "Queries sent: *2" test2.out
+../dnsperf -s $ip -d "$srcdir/datafile" -n 1 -e -D >test2.out
+cat test2.out
+grep -q "Queries sent: *2" test2.out
+
+../dnsperf -d "$srcdir/updatefile" -u -s $ip -y hmac-md5:test:Ax42vsuHBjQOKlVHO8yU1zGuQ5hjeSz01LXiNze8pb8= >test2.out
+cat test2.out
+grep -q "Updates sent: *1" test2.out
+../dnsperf -d "$srcdir/updatefile" -u -s $ip -y hmac-sha1:test:Ax42vsuHBjQOKlVHO8yU1zGuQ5hjeSz01LXiNze8pb8= >test2.out
+cat test2.out
+grep -q "Updates sent: *1" test2.out
+../dnsperf -d "$srcdir/updatefile" -u -s $ip -y hmac-sha224:test:Ax42vsuHBjQOKlVHO8yU1zGuQ5hjeSz01LXiNze8pb8= >test2.out
+cat test2.out
+grep -q "Updates sent: *1" test2.out
+../dnsperf -d "$srcdir/updatefile" -u -s $ip -y hmac-sha256:test:Ax42vsuHBjQOKlVHO8yU1zGuQ5hjeSz01LXiNze8pb8= >test2.out
+cat test2.out
+grep -q "Updates sent: *1" test2.out
+../dnsperf -d "$srcdir/updatefile" -u -s $ip -y hmac-sha384:test:Ax42vsuHBjQOKlVHO8yU1zGuQ5hjeSz01LXiNze8pb8= >test2.out
+cat test2.out
+grep -q "Updates sent: *1" test2.out
+../dnsperf -d "$srcdir/updatefile" -u -s $ip -y hmac-sha512:test:Ax42vsuHBjQOKlVHO8yU1zGuQ5hjeSz01LXiNze8pb8= >test2.out
+cat test2.out
+grep -q "Updates sent: *1" test2.out
+
+../resperf -s $ip -m 1 -d "$srcdir/datafile2" -r 2 -c 2 -M udp >test2.out
+cat test2.out
+grep -q "Queries sent: *2" test2.out
+../resperf -s $ip -m 1 -d "$srcdir/datafile2" -r 2 -c 2 -M tcp >test2.out
+cat test2.out
+grep -q "Queries sent: *2" test2.out
+
+../resperf -s $ip -m 1 -d "$srcdir/datafile2" -r 2 -c 2 -M udp -D >test2.out
+cat test2.out
+grep -q "Queries sent: *2" test2.out
+# Disabled until https://github.com/DNS-OARC/dnsperf/issues/92 is fixed
+../resperf -s $ip -m 1 -d "$srcdir/datafile2" -r 2 -c 2 -M udp -y hmac-sha256:test:Ax42vsuHBjQOKlVHO8yU1zGuQ5hjeSz01LXiNze8pb8= >test2.out
+cat test2.out
+grep -q "Queries sent: *2" test2.out
+
+# Ignore failure until https://github.com/DNS-OARC/dnsperf/issues/88 is fixed
+# May work on slower systems
+../resperf -s $ip -m 1 -d "$srcdir/datafile2" -r 2 -c 2 -M dot || true
+
+# TYPE test
+../dnsperf -s $ip -d "$srcdir/datafile5" -n 1 -W >test2.out
+cat test2.out
+grep -q "Queries sent: *4" test2.out
+grep -q "Warning: invalid qtype: TYPE99999999" test2.out
+
+done # for ip
+
+../dnsperf -s 127.66.66.66 -d "$srcdir/datafile" -vvvv -m tcp -n 1 &
+sleep 2
+pkill -KILL -u `id -u` dnsperf || true
+
+../dnsperf -s 127.66.66.66 -d "$srcdir/datafile" -vvvv -m dot -n 1 &
+sleep 2
+pkill -KILL -u `id -u` dnsperf || true
+
+! echo "google.com A" \
+ | ../dnsperf -W -s 1.1.1.1 -y tooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong:Ax42vsuHBjQOKlVHO8yU1zGuQ5hjeSz01LXiNze8pb8= \
+ | grep "adding TSIG: invalid owner name"
+echo ".google.com A" | ../dnsperf -W -s 1.1.1.1 \
+ | grep "invalid domain name"
+echo "google.com.. A" | ../dnsperf -W -s 1.1.1.1 \
+ | grep "invalid domain name"
+echo " A" | ../dnsperf -W -s 1.1.1.1 \
+ | grep "invalid query input format"
+echo "toooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooolongname" \
+ | ../dnsperf -W -s 1.1.1.1 -u \
+ | grep "Unable to parse domain name"
+echo "tooooooooooooooooooooooooooooo.oooooooooooooooooooooooo.ooooooooooooooooooooooooooooo.ooooooooooooooooooooooooooooooo.oooooooooooooooooooooooooooooooooooooo.oooooooooooooooooooooooooooo.ooooooooooooooooooooooooo.ooooooooooooooooooooooooooooooooo.oooooooooooooooooooooooooooo.oooooooooooooooooooooooooooooooo.ooooooooooooooooooo.longname" \
+ | ../dnsperf -W -s 1.1.1.1 -u \
+ | grep "Unable to parse domain name"
+echo -e "test\ndelete toooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooolongname" \
+ | ../dnsperf -W -s 1.1.1.1 -u \
+ | grep "invalid update command, domain name too large"
--- /dev/null
+#!/bin/sh -xe
+
+! ../dnsperf -d does_not_exist
+! ../resperf -d does_not_exist
+! ../dnsperf -f invalid
+! ../dnsperf -f any -s 256.256.256.256
+! ../dnsperf -f inet -s 256.256.256.256
+! ../dnsperf -f inet6 -s 256.256.256.256
+! ../dnsperf -a 127.0.0.1 -d does_not_exist
+! ../dnsperf -a ::1 -d does_not_exist
+! ../dnsperf -a 256.256.256.256
+! ../dnsperf -m invalid
+! ../dnsperf -n 43f8huishfs
+! ../dnsperf -p 12345 unexpected argument
+! ../dnsperf -p 65536
+
+! echo "" | ../dnsperf -y test:Ax42vsuHBjQOKlVHO8yU1zGuQ5hjeSz01LXiNze8pb8=
+! echo "" | ../dnsperf -y hmac-md5:test:Ax42vsuHBjQOKlVHO8yU1zGuQ5hjeSz01LXiNze8pb8=
+! echo "" | ../dnsperf -y hmac-sha1:test:Ax42vsuHBjQOKlVHO8yU1zGuQ5hjeSz01LXiNze8pb8=
+! echo "" | ../dnsperf -y hmac-sha224:test:Ax42vsuHBjQOKlVHO8yU1zGuQ5hjeSz01LXiNze8pb8=
+! echo "" | ../dnsperf -y hmac-sha256:test:Ax42vsuHBjQOKlVHO8yU1zGuQ5hjeSz01LXiNze8pb8=
+! echo "" | ../dnsperf -y hmac-sha384:test:Ax42vsuHBjQOKlVHO8yU1zGuQ5hjeSz01LXiNze8pb8=
+! echo "" | ../dnsperf -y hmac-sha512:test:Ax42vsuHBjQOKlVHO8yU1zGuQ5hjeSz01LXiNze8pb8=
+! echo "" | ../dnsperf -y invalid:test:Ax42vsuHBjQOKlVHO8yU1zGuQ5hjeSz01LXiNze8pb8=
+! echo "" | ../dnsperf -y test:invalid
+! echo "" | ../dnsperf -y test
+echo "" | ../dnsperf -W -y toooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooolongname:Ax42vsuHBjQOKlVHO8yU1zGuQ5hjeSz01LXiNze8pb8= \
+ | grep "unable to setup TSIG, name too long"
+echo "" | ../dnsperf -W -y test: | grep "unable to setup TSIG, secret empty"
+
+! ../dnsperf -e -E invalid
+! ../dnsperf -e -E 9999999:invalid
+! ../dnsperf -e -E 123:invalid
+! ../dnsperf -e -E 123:fa0
+../dnsperf -W -E a: | grep "invalid EDNS Option, value is empty"
+../dnsperf -W -E a:a | grep "invalid EDNS Option, value must hex string (even number of characters)"
+../dnsperf -W -E a:aa | grep "invalid EDNS Option code 'a'"
+../dnsperf -W -E 1:xx | grep "invalid EDNS Option hex value 'xx'"
+
+! ../resperf -e -E invalid
+! ../resperf -e -E 9999999:invalid
+! ../resperf -e -E 123:invalid
+! ../resperf -e -E 123:fa0
+../resperf -W -E a: | grep "invalid EDNS Option, value is empty"
+../resperf -W -E a:a | grep "invalid EDNS Option, value must hex string (even number of characters)"
+../resperf -W -E a:aa | grep "invalid EDNS Option code 'a'"
+../resperf -W -E 1:xx | grep "invalid EDNS Option hex value 'xx'"
+
+! ../resperf -d does_not_exist
+! ../resperf -r 0 -c 0
+! ../resperf -f invalid
+! ../resperf -q 256000
+! ../resperf -m 123.45 unexpected argument
+! ../resperf -m 123..
+! ../resperf -m 123a
+
+echo "invalid" | ../dnsperf -W | grep "invalid query input format: invalid"
+echo "invalid invalid" | ../dnsperf -W | grep "invalid qtype: invalid"
+if ! echo "invalid" | ../dnsperf -u -W | grep "Unable to dynamic update, support not built in"; then
+ echo "invalid" | ../dnsperf -u -W | grep "incomplete update: invalid"
+ echo -e "invalid\ninvalid" | ../dnsperf -u -W | grep "invalid update command: invalid"
+ echo -e "invalid\ninvalid" | ../dnsperf -u -W | grep "error processing update command: invalid"
+fi
--- /dev/null
+#!/bin/sh -xe
+
+test "$TEST_DNSPERF_WITH_NETWORK" = "1" || exit 0
+
+../dnsperf -vvv -d "$srcdir/datafile4" -t 0 -s 127.0.0.1 >test4.out 2>test4err.out
+
+grep 'api-read.facebook.com.\\002\\004\\003\\002\\002\\002\\002\\005\\004\\004\\003\\004\\006\\005\\006\\006\\006\\005\\006\\006\\006\\007\\009\\008\\006\\007\\009\\007\\006\\006\\008\\011\\008\\009\\010\\010\\010\\010\\010\\006\\008\\011\\012\\011\\010\\012\\009\\010\\010\\010\\255\\219. A' test4.out
+grep 'T valid\\.quote.com A' test4.out
+grep 'Warning: invalid domain name (or out of space): invalid\\0quote.com' test4err.out
--- /dev/null
+#!/bin/sh -xe
+
+test "$TEST_DNSPERF_WITH_NETWORK" = "1" || exit 0
+
+dumdumd=`which dumdumd`
+dumdohd=`which dumdohd`
+
+pkill -9 dumdumd || true
+pkill -9 dumdohd || true
+
+if [ -n "$dumdumd" ]; then
+ $dumdumd 127.0.0.1 5353 -r -D 100 &
+ pid="$!"
+ sleep 2
+ ../dnsperf -s 127.0.0.1 -p 5353 -d "$srcdir/datafile" -t 2 -l 2 -Q 10 -m tcp
+ kill "$pid"
+ wait "$pid" || true
+
+ $dumdumd 127.0.0.1 5353 -r -D 10 &
+ pid="$!"
+ sleep 2
+ ../dnsperf -s 127.0.0.1 -p 5353 -d "$srcdir/datafile" -t 2 -l 10 -Q 100 -m tcp
+ kill "$pid"
+ wait "$pid" || true
+
+ rm -f key.pem cert.pem
+ openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes -subj "/C=AU/ST=Some-State/O=Internet Widgits Pty Ltd"
+
+ $dumdumd 127.0.0.1 5353 -r -T -D 100 &
+ pid="$!"
+ sleep 2
+ ../dnsperf -s 127.0.0.1 -p 5353 -d "$srcdir/datafile" -t 2 -l 2 -Q 10 -m dot
+ kill "$pid"
+ wait "$pid" || true
+
+ $dumdumd 127.0.0.1 5353 -r -T -D 10 &
+ pid="$!"
+ sleep 2
+ ../dnsperf -s 127.0.0.1 -p 5353 -d "$srcdir/datafile" -t 2 -l 10 -Q 100 -m dot
+ kill "$pid"
+ wait "$pid" || true
+fi
+
+if [ -n "$dumdohd" ]; then
+ rm -f key.pem cert.pem
+ openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes -subj "/C=AU/ST=Some-State/O=Internet Widgits Pty Ltd"
+
+ $dumdohd 5354 key.pem cert.pem -D 100 &
+ pid="$!"
+ sleep 2
+ ../dnsperf -s 127.0.0.1 -p 5354 -d "$srcdir/datafile" -t 2 -l 2 -Q 10 -m doh
+ kill "$pid"
+ wait "$pid" || true
+
+ $dumdohd 5354 key.pem cert.pem -D 10 &
+ pid="$!"
+ sleep 2
+ ../dnsperf -s 127.0.0.1 -p 5354 -d "$srcdir/datafile" -t 2 -l 10 -Q 100 -m doh
+ kill "$pid"
+ wait "$pid" || true
+fi
--- /dev/null
+#!/bin/sh -xe
+
+test "$TEST_DNSPERF_WITH_NETWORK" = "1" || exit 0
+
+../dnsperf -vvv -d "$srcdir/datafile6" -s 1.1.1.1 >test6.out
+
+grep "NXDOMAIN 26" test6.out
--- /dev/null
+#!/bin/sh -xe
+
+# expect non-zero exit code
+malformed_input_fmt() {
+ FILESTEM="$1"
+ ! ../dnsperf -vvv -B -d "$srcdir/$FILESTEM.blob" > "$FILESTEM.out" 2>&1
+ grep -F "Error: input file contains no data" "$FILESTEM.out"
+}
+
+malformed_input_fmt "empty"
+malformed_input_fmt "tooshortlength"
+malformed_input_fmt "missingpayload"
+
+test "$TEST_DNSPERF_WITH_NETWORK" = "1" || exit 0
+
+check_sent_and_lost() {
+ FILESTEM="$1"
+ EXPECTEDCOUNT="$2"
+ grep "Queries sent: $EXPECTEDCOUNT$" "$FILESTEM.out"
+ grep -F "Queries lost: $EXPECTEDCOUNT (" "$FILESTEM.out"
+}
+
+# send to an address which does not reply anyway;
+# typically for weird blobs which do not even have DNS header - so we cannot expect a response
+blackhole() {
+ FILESTEM="$1"
+ EXTRAARGS="$2"
+ EXPECTEDCOUNT="$3"
+ ../dnsperf -t 0.001 -vvv -B -d "$srcdir/$FILESTEM.blob" -s 192.0.2.1 $EXTRAARGS > "$FILESTEM.out" 2>&1
+ check_sent_and_lost "$FILESTEM" "$EXPECTEDCOUNT"
+}
+blackhole2() {
+ FILESTEM="$1"
+ EXTRAARGS="$2"
+ EXPECTEDCOUNT="$3"
+ ../dnsperf -t 0.001 -vvv -B -d "$FILESTEM.blob" -s 192.0.2.1 $EXTRAARGS > "$FILESTEM.out" 2>&1
+ check_sent_and_lost "$FILESTEM" "$EXPECTEDCOUNT"
+}
+
+blackhole "emptypayload" "" 1
+blackhole "shortpayload" "" 1
+blackhole "largestudp" "" 1
+# too large for UDP; at least it should not crash
+blackhole "largesttcp" "" 0
+grep -F 'failed to send packet' largesttcp.out
+
+# valid DNS queries as blobs
+expect_noerror() {
+ FILESTEM="$1"
+ EXTRAARGS="$2"
+ EXPECTEDCOUNT="$3"
+ ../dnsperf -vvv -B -d "$srcdir/$FILESTEM.blob" -s 1.1.1.1 $EXTRAARGS > "$FILESTEM.out" 2>&1
+ grep "Queries sent: $EXPECTEDCOUNT$" "$FILESTEM.out"
+ grep -F "Queries completed: $EXPECTEDCOUNT (" "$FILESTEM.out"
+}
+
+# single plain run
+expect_noerror "querywithcookie" "" 1
+
+# loop over the binary twice
+expect_noerror "querywithcookie" "-n 2" 2
+
+# multiple queries in one file
+expect_noerror "twoquerieswithnsid" "" 2
+
+# file too big to cache
+rm -f 10queries.tmp.blob
+cat "$srcdir/twoquerieswithnsid.blob" "$srcdir/querywithcookie.blob" "$srcdir/emptypayload.blob" \
+ "$srcdir/largestudp.blob" "$srcdir/twoquerieswithnsid.blob" "$srcdir/querywithcookie.blob" \
+ "$srcdir/emptypayload.blob" "$srcdir/largestudp.blob" \
+ > 10queries.tmp.blob
+blackhole2 "10queries.tmp" "" 10
+
+# repeat non-cacheable file the same twice
+blackhole2 "10queries.tmp" "-n 2" 20
+
+# large binary on stdin should work too
+cat 10queries.tmp.blob | ../dnsperf -t 0.001 -vvv -B -s 192.0.2.1 > "stdinlarge.out" 2>&1
+check_sent_and_lost "stdinlarge" 10
+
+# small binary on stdin
+cat "$srcdir/twoquerieswithnsid.blob" | ../dnsperf -t 0.001 -vvv -B -s 192.0.2.1 > "stdinsmall.out" 2>&1
+check_sent_and_lost "stdinsmall" 2
--- /dev/null
+example.com
+require a
+require a A
+require a A 1.2.3.4
+prohibit x
+prohibit x A
+add x 3600 A 10.1.2.3
+delete y A 10.1.2.3
+delete z A
+delete w
+send
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "config.h"
+
+#include "tsig.h"
+
+#include "log.h"
+#include "opt.h"
+#include "dns.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <limits.h>
+#include <openssl/evp.h>
+#include <time.h>
+
+#define TSIG_HMACMD5_NAME "hmac-md5.sig-alg.reg.int"
+#define TSIG_HMACSHA1_NAME "hmac-sha1"
+#define TSIG_HMACSHA224_NAME "hmac-sha224"
+#define TSIG_HMACSHA256_NAME "hmac-sha256"
+#define TSIG_HMACSHA384_NAME "hmac-sha384"
+#define TSIG_HMACSHA512_NAME "hmac-sha512"
+
+static unsigned char* decode64(const void* base64, int* len)
+{
+ unsigned char* out;
+
+ assert(base64);
+ assert(len);
+ assert(*len);
+
+ out = calloc(1, *len);
+ assert(out);
+
+ int olen = *len;
+
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
+ EVP_ENCODE_CTX evp;
+ EVP_DecodeInit(&evp);
+ if (EVP_DecodeUpdate(&evp, out, &olen, base64, *len)) {
+ free(out);
+ return 0;
+ }
+#else
+ EVP_ENCODE_CTX* evp = EVP_ENCODE_CTX_new();
+ if (!evp) {
+ free(out);
+ return 0;
+ }
+ EVP_DecodeInit(evp);
+ if (EVP_DecodeUpdate(evp, out, &olen, base64, *len)) {
+ free(out);
+ EVP_ENCODE_CTX_free(evp);
+ return 0;
+ }
+ EVP_ENCODE_CTX_free(evp);
+#endif
+
+ *len = olen;
+
+ return out;
+}
+
+perf_tsigkey_t* perf_tsig_parsekey(const char* arg)
+{
+ perf_tsigkey_t* tsigkey;
+ const char * sep1, *sep2, *alg, *name, *secret;
+ size_t alglen, namelen, secretlen;
+ int keylen;
+ const EVP_MD* md = 0;
+
+ tsigkey = calloc(1, sizeof(*tsigkey));
+ if (!tsigkey) {
+ perf_log_fatal("out of memory");
+ return 0; // fix clang scan-build
+ }
+
+ sep1 = strchr(arg, ':');
+ if (sep1 == NULL) {
+ perf_log_warning("invalid TSIG [alg:]name:secret");
+ perf_opt_usage();
+ exit(1);
+ }
+
+ sep2 = strchr(sep1 + 1, ':');
+ if (sep2 == NULL) {
+ /* name:key */
+ alg = NULL;
+ alglen = 0;
+ name = arg;
+ namelen = sep1 - arg;
+ secret = sep1 + 1;
+ } else {
+ /* [alg:]name:secret */
+ alg = arg;
+ alglen = sep1 - arg;
+ name = sep1 + 1;
+ namelen = sep2 - sep1 - 1;
+ secret = sep2 + 1;
+ }
+
+ /* Algorithm */
+
+ if (!alg || !strncasecmp(alg, "hmac-md5:", 9)) {
+ md = EVP_md5();
+ tsigkey->alg = TSIG_HMACMD5_NAME;
+ tsigkey->alglen = sizeof(TSIG_HMACMD5_NAME) - 1;
+ } else if (!strncasecmp(alg, "hmac-sha1:", 10)) {
+ md = EVP_sha1();
+ tsigkey->alg = TSIG_HMACSHA1_NAME;
+ tsigkey->alglen = sizeof(TSIG_HMACSHA1_NAME) - 1;
+ } else if (!strncasecmp(alg, "hmac-sha224:", 12)) {
+ md = EVP_sha224();
+ tsigkey->alg = TSIG_HMACSHA224_NAME;
+ tsigkey->alglen = sizeof(TSIG_HMACSHA224_NAME) - 1;
+ } else if (!strncasecmp(alg, "hmac-sha256:", 12)) {
+ md = EVP_sha256();
+ tsigkey->alg = TSIG_HMACSHA256_NAME;
+ tsigkey->alglen = sizeof(TSIG_HMACSHA256_NAME) - 1;
+ } else if (!strncasecmp(alg, "hmac-sha384:", 12)) {
+ md = EVP_sha384();
+ tsigkey->alg = TSIG_HMACSHA384_NAME;
+ tsigkey->alglen = sizeof(TSIG_HMACSHA384_NAME) - 1;
+ } else if (!strncasecmp(alg, "hmac-sha512:", 12)) {
+ md = EVP_sha512();
+ tsigkey->alg = TSIG_HMACSHA512_NAME;
+ tsigkey->alglen = sizeof(TSIG_HMACSHA512_NAME) - 1;
+ } else {
+ perf_log_warning("invalid TSIG algorithm %.*s", (int)alglen, alg);
+ perf_opt_usage();
+ exit(1);
+ }
+
+ if (namelen > sizeof(tsigkey->name)) {
+ perf_log_fatal("unable to setup TSIG, name too long");
+ // fix clang scan-build / sonarcloud:
+ free(tsigkey);
+ return 0;
+ }
+ memcpy(tsigkey->name, name, namelen);
+ tsigkey->namelen = namelen;
+ for (namelen = 0; namelen < tsigkey->namelen; namelen++) {
+ tsigkey->name[namelen] = tolower(tsigkey->name[namelen]);
+ }
+
+ /* Secret */
+
+ secretlen = strlen(secret);
+ if (!secretlen) {
+ perf_log_warning("unable to setup TSIG, secret empty");
+ perf_opt_usage();
+ exit(1);
+ }
+
+ keylen = secretlen;
+ unsigned char* key = decode64(secret, &keylen);
+ if (!key) {
+ perf_log_fatal("unable to setup TSIG, invalid base64 secret");
+ }
+
+ /* Setup HMAC */
+
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
+ if (!(tsigkey->hmac = calloc(1, sizeof(*tsigkey->hmac)))) {
+ perf_log_fatal("unable to setup TSIG, OpenSSL HMAC context failed to be created");
+ }
+ HMAC_CTX_init(tsigkey->hmac);
+#elif OPENSSL_VERSION_NUMBER < 0x30000000L
+ if (!(tsigkey->hmac = HMAC_CTX_new())) {
+ perf_log_fatal("unable to setup TSIG, OpenSSL HMAC context failed to be created");
+ }
+#else
+ if (!(tsigkey->pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, 0, key, keylen))) {
+ perf_log_fatal("unable to setup TSIG, OpenSSL EVP PKEY failed to be created");
+ }
+ if (!(tsigkey->mdctx = EVP_MD_CTX_create())) {
+ perf_log_fatal("unable to setup TSIG, OpenSSL EVP MD context failed to be created");
+ }
+ if (!EVP_DigestSignInit(tsigkey->mdctx, 0, md, 0, tsigkey->pkey)) {
+ perf_log_fatal("unable to setup TSIG, OpenSSL EVP DigestSign init failed");
+ }
+#endif
+
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
+ if (!HMAC_Init_ex(tsigkey->hmac, key, keylen, md, 0)) {
+ perf_log_fatal("unable to setup TSIG, OpenSSL HMAC init failed");
+ }
+#endif
+
+ free(key);
+
+ return tsigkey;
+}
+
+void perf_tsig_destroykey(perf_tsigkey_t** tsigkeyp)
+{
+ assert(tsigkeyp);
+ assert(*tsigkeyp);
+
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
+ HMAC_CTX_cleanup((*tsigkeyp)->hmac);
+ free((*tsigkeyp)->hmac);
+#elif OPENSSL_VERSION_NUMBER < 0x30000000L
+ HMAC_CTX_free((*tsigkeyp)->hmac);
+#else
+ EVP_MD_CTX_free((*tsigkeyp)->mdctx);
+ EVP_PKEY_free((*tsigkeyp)->pkey);
+#endif
+
+ free(*tsigkeyp);
+ *tsigkeyp = 0;
+}
+
+/*
+ * Appends a TSIG record to the packet.
+ */
+perf_result_t perf_add_tsig(perf_buffer_t* packet, perf_tsigkey_t* tsigkey)
+{
+ unsigned char* base;
+ size_t rdlen, totallen;
+ unsigned char tmpdata[512], md[EVP_MAX_MD_SIZE];
+ perf_buffer_t tmp;
+ uint32_t now;
+ perf_result_t result;
+
+ now = time(NULL);
+
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
+ if (!HMAC_Init_ex(tsigkey->hmac, 0, 0, 0, 0)) {
+ perf_log_fatal("adding TSIG: OpenSSL HMAC reinit failed");
+ }
+ if (!HMAC_Update(tsigkey->hmac, perf_buffer_base(packet), perf_buffer_usedlength(packet))) {
+ perf_log_fatal("adding TSIG: OpenSSL HMAC update failed");
+ }
+#else
+ if (!EVP_DigestSignInit(tsigkey->mdctx, 0, 0, 0, 0)) {
+ perf_log_fatal("adding TSIG: OpenSSL EVP DigestSign reinit failed");
+ }
+ if (!EVP_DigestSignUpdate(tsigkey->mdctx, perf_buffer_base(packet), perf_buffer_usedlength(packet))) {
+ perf_log_fatal("adding TSIG: OpenSSL EVP DigestSign update failed");
+ }
+#endif
+
+ /* Digest the TSIG record */
+ perf_buffer_init(&tmp, tmpdata, sizeof tmpdata);
+ switch ((result = perf_dname_fromstring(tsigkey->name, tsigkey->namelen, &tmp))) {
+ case PERF_R_SUCCESS:
+ break;
+ case PERF_R_NOSPACE:
+ perf_log_warning("adding TSIG: out of space in digest record");
+ return result;
+ default:
+ perf_log_warning("adding TSIG: invalid owner name");
+ return result;
+ }
+ perf_buffer_putuint16(&tmp, 255); /* class ANY */
+ perf_buffer_putuint32(&tmp, 0); /* ttl */
+ switch ((result = perf_dname_fromstring(tsigkey->alg, tsigkey->alglen, &tmp))) {
+ case PERF_R_SUCCESS:
+ break;
+ case PERF_R_NOSPACE:
+ perf_log_warning("adding TSIG: out of space in digest record");
+ return result;
+ default:
+ perf_log_warning("adding TSIG: invalid algorithm name");
+ return result;
+ }
+ perf_buffer_putuint16(&tmp, 0); /* time high */
+ perf_buffer_putuint32(&tmp, now); /* time low */
+ perf_buffer_putuint16(&tmp, 300); /* fudge */
+ perf_buffer_putuint16(&tmp, 0); /* error */
+ perf_buffer_putuint16(&tmp, 0); /* other length */
+
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
+ unsigned int mdlen = sizeof(md);
+ if (!HMAC_Update(tsigkey->hmac, perf_buffer_base(&tmp), perf_buffer_usedlength(&tmp))) {
+ perf_log_fatal("adding TSIG: OpenSSL HMAC update failed");
+ }
+ if (!HMAC_Final(tsigkey->hmac, md, &mdlen)) {
+ perf_log_fatal("adding TSIG: OpenSSL HMAC final failed");
+ }
+#else
+ size_t mdlen = sizeof(md);
+ if (!EVP_DigestSignUpdate(tsigkey->mdctx, perf_buffer_base(&tmp), perf_buffer_usedlength(&tmp))) {
+ perf_log_fatal("adding TSIG: OpenSSL EVP DigestSign update failed");
+ }
+ if (!EVP_DigestSignFinal(tsigkey->mdctx, md, &mdlen)) {
+ perf_log_fatal("adding TSIG: OpenSSL EVP DigestSign final failed");
+ }
+#endif
+
+ /* Make sure everything will fit */
+ rdlen = tsigkey->alglen + 18 + mdlen;
+ totallen = tsigkey->namelen + 12 + rdlen;
+ if (totallen > perf_buffer_availablelength(packet)) {
+ perf_log_warning("adding TSIG: out of space");
+ return PERF_R_NOSPACE;
+ }
+
+ base = perf_buffer_base(packet);
+
+ /* Add the TSIG record. */
+ switch ((result = perf_dname_fromstring(tsigkey->name, tsigkey->namelen, packet))) {
+ case PERF_R_SUCCESS:
+ break;
+ case PERF_R_NOSPACE:
+ perf_log_warning("adding TSIG: out of space");
+ return result;
+ default:
+ perf_log_warning("adding TSIG: invalid owner name");
+ return result;
+ }
+ perf_buffer_putuint16(packet, 250); /* type TSIG */
+ perf_buffer_putuint16(packet, 255); /* class ANY */
+ perf_buffer_putuint32(packet, 0); /* ttl */
+ perf_buffer_putuint16(packet, rdlen); /* rdlen */
+ switch ((result = perf_dname_fromstring(tsigkey->alg, tsigkey->alglen, packet))) {
+ case PERF_R_SUCCESS:
+ break;
+ case PERF_R_NOSPACE:
+ perf_log_warning("adding TSIG: out of space");
+ return result;
+ default:
+ perf_log_warning("adding TSIG: invalid algorithm name");
+ return result;
+ }
+ perf_buffer_putuint16(packet, 0); /* time high */
+ perf_buffer_putuint32(packet, now); /* time low */
+ perf_buffer_putuint16(packet, 300); /* fudge */
+ perf_buffer_putuint16(packet, mdlen); /* digest len */
+ perf_buffer_putmem(packet, md, mdlen); /* digest */
+ perf_buffer_putmem(packet, base, 2); /* orig ID */
+ perf_buffer_putuint16(packet, 0); /* error */
+ perf_buffer_putuint16(packet, 0); /* other len */
+
+ base[11]++; /* increment additional record count */
+
+ return PERF_R_SUCCESS;
+}
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "result.h"
+#include "buffer.h"
+
+#ifndef PERF_TSIG_H
+#define PERF_TSIG_H 1
+
+#include <openssl/opensslv.h>
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
+#include <openssl/hmac.h>
+#else
+#include <openssl/evp.h>
+#endif
+
+typedef struct perf_tsigkey {
+ char name[256];
+ size_t namelen, alglen;
+ const char* alg;
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
+ HMAC_CTX* hmac;
+#else
+ EVP_PKEY* pkey;
+ EVP_MD_CTX* mdctx;
+#endif
+} perf_tsigkey_t;
+
+perf_tsigkey_t* perf_tsig_parsekey(const char* arg);
+
+void perf_tsig_destroykey(perf_tsigkey_t** tsigkeyp);
+
+perf_result_t perf_add_tsig(perf_buffer_t* packet, perf_tsigkey_t* tsigkey);
+
+#endif
--- /dev/null
+/*
+ * Copyright 2019-2026 OARC, Inc.
+ * Copyright 2017-2018 Akamai Technologies
+ * Copyright 2006-2016 Nominum, Inc.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "log.h"
+#include "strerror.h"
+
+#ifndef PERF_UTIL_H
+#define PERF_UTIL_H 1
+
+#include <pthread.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <string.h>
+#include <sys/time.h>
+#include <errno.h>
+
+#define MILLION ((uint64_t)1000000)
+
+#define PERF_THREAD(thread, start, arg) \
+ do { \
+ int __n = pthread_create((thread), NULL, (start), (arg)); \
+ if (__n != 0) { \
+ char __s[256]; \
+ perf_log_fatal("pthread_create failed: %s", perf_strerror_r(__n, __s, sizeof(__s))); \
+ } \
+ } while (0)
+
+#define PERF_JOIN(thread, valuep) \
+ do { \
+ int __n = pthread_join((thread), (valuep)); \
+ if (__n != 0) { \
+ char __s[256]; \
+ perf_log_fatal("pthread_join failed: %s", perf_strerror_r(__n, __s, sizeof(__s))); \
+ } \
+ } while (0)
+
+#define PERF_MUTEX_INIT(mutex) \
+ do { \
+ int __n = pthread_mutex_init((mutex), NULL); \
+ if (__n != 0) { \
+ char __s[256]; \
+ perf_log_fatal("pthread_mutex_init failed: %s", perf_strerror_r(__n, __s, sizeof(__s))); \
+ } \
+ } while (0)
+
+#define PERF_MUTEX_DESTROY(mutex) \
+ do { \
+ int __n = pthread_mutex_destroy((mutex)); \
+ if (__n != 0) { \
+ char __s[256]; \
+ perf_log_fatal("pthread_mutex_destroy failed: %s", perf_strerror_r(__n, __s, sizeof(__s))); \
+ } \
+ } while (0)
+
+#define PERF_LOCK(mutex) \
+ do { \
+ int __n = pthread_mutex_lock((mutex)); \
+ if (__n != 0) { \
+ char __s[256]; \
+ perf_log_fatal("pthread_mutex_lock failed: %s", perf_strerror_r(__n, __s, sizeof(__s))); \
+ } \
+ } while (0)
+
+static inline int PERF_TRYLOCK(pthread_mutex_t* mutex)
+{
+ int __n = pthread_mutex_trylock(mutex);
+ if (__n == EBUSY) {
+ return 1;
+ }
+ if (__n != 0) {
+ char __s[256];
+ perf_log_fatal("pthread_mutex_lock failed: %s", perf_strerror_r(__n, __s, sizeof(__s)));
+ }
+ return 0;
+}
+
+#define PERF_UNLOCK(mutex) \
+ do { \
+ int __n = pthread_mutex_unlock((mutex)); \
+ if (__n != 0) { \
+ char __s[256]; \
+ perf_log_fatal("pthread_mutex_unlock failed: %s", perf_strerror_r(__n, __s, sizeof(__s))); \
+ } \
+ } while (0)
+
+#define PERF_COND_INIT(cond) \
+ do { \
+ int __n = pthread_cond_init((cond), NULL); \
+ if (__n != 0) { \
+ char __s[256]; \
+ perf_log_fatal("pthread_cond_init failed: %s", perf_strerror_r(__n, __s, sizeof(__s))); \
+ } \
+ } while (0)
+
+#define PERF_SIGNAL(cond) \
+ do { \
+ int __n = pthread_cond_signal((cond)); \
+ if (__n != 0) { \
+ char __s[256]; \
+ perf_log_fatal("pthread_cond_signal failed: %s", perf_strerror_r(__n, __s, sizeof(__s))); \
+ } \
+ } while (0)
+
+#define PERF_BROADCAST(cond) \
+ do { \
+ int __n = pthread_cond_broadcast((cond)); \
+ if (__n != 0) { \
+ char __s[256]; \
+ perf_log_fatal("pthread_cond_broadcast failed: %s", perf_strerror_r(__n, __s, sizeof(__s))); \
+ } \
+ } while (0)
+
+#define PERF_WAIT(cond, mutex) \
+ do { \
+ int __n = pthread_cond_wait((cond), (mutex)); \
+ if (__n != 0) { \
+ char __s[256]; \
+ perf_log_fatal("pthread_cond_wait failed: %s", perf_strerror_r(__n, __s, sizeof(__s))); \
+ } \
+ } while (0)
+
+#define PERF_TIMEDWAIT(cond, mutex, when, timedout) \
+ do { \
+ int __n = pthread_cond_timedwait((cond), (mutex), (when)); \
+ bool* res = (timedout); \
+ if (__n != 0 && __n != ETIMEDOUT) { \
+ char __s[256]; \
+ perf_log_fatal("pthread_cond_timedwait failed: %s", perf_strerror_r(__n, __s, sizeof(__s))); \
+ } \
+ if (res != NULL) { \
+ *res = (__n != 0); \
+ } \
+ } while (0)
+
+static __inline__ uint64_t perf_get_time(void)
+{
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ return tv.tv_sec * MILLION + tv.tv_usec;
+}
+
+#define PERF_SAFE_DIV(n, d) ((d) == 0 ? 0 : (n) / (d))
+
+#endif